user786731
user786731

Reputation: 171

Create a ZIP file download and then delete it

I am looking to create a PHP script which will create a zip file from a location which will be a variable passed via $_GET. The location will be a folder which will then be zipped up and the user will be prompted to download the folder, after download the folder will need to automatically be deleted.

Can anyone help?

Thanks

Upvotes: 1

Views: 5865

Answers (2)

Harish Singh
Harish Singh

Reputation: 3329

read the location from $_GET and read all files under and create an array of files. than create a zip and download.

following link will help you http://www.tricksofit.com/2013/10/create-zip-file-php

Upvotes: 0

blejzz
blejzz

Reputation: 3349

http://php.net/manual/en/ref.zip.php

You could do something like this:

  • validate get
  • look up if the folder exists
  • zip the folder
  • read the newly created zip file and delete it at the end, like this:

       header('Content-Description: File Transfer');
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename='.basename($file));
       header('Content-Transfer-Encoding: binary');
       header('Expires: 0');
       header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
       header('Pragma: public');
       header('Content-Length: ' . filesize($file));
       ob_clean();
       flush();
       readfile($file); 
       @unlink($file);
    

Code taken frome here: http://php.net/manual/en/function.readfile.php

Upvotes: 4

Related Questions