How to delete zip file after download complete using php?

Member

by darion , in category: PHP General , 6 months ago

How to delete zip file after download complete using php?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by deron , 6 months ago

@darion 

To delete a ZIP file after it has been downloaded using PHP, you can use the unlink() function to delete the file from the server.


Here's an example of how you can achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
$zipFilePath = "/path/to/your/zip/file.zip"; // Specify the path to your ZIP file

// Logic to generate and download the ZIP file
// ...

// Delete the ZIP file after download
if (file_exists($zipFilePath)) {
    unlink($zipFilePath);
    echo "ZIP file deleted successfully.";
} else {
    echo "ZIP file not found.";
}
?>


Make sure to replace /path/to/your/zip/file.zip with the actual path to your ZIP file.


First, you should specify the path to the ZIP file using the $zipFilePath variable. Then, after the ZIP file has been downloaded, you can check if the file exists using file_exists(). If it exists, you can use unlink() to delete the file from the server. Finally, you can display a success message if the file was deleted successfully, or an error message if the file was not found.


Note: Make sure that the PHP script has the necessary file system permissions to delete files.

Member

by deron , 6 months ago

@darion 

Please note that the unlink() function deletes the file from the server permanently, so use it with caution.