Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a ZIP file using PHP and delete it after user downloads it?

Tags:

php

download

zip

I need to download images from other websites to my server. Create a ZIP file with those images. automatically start download of created ZIP file. once download is complete the ZIP file and images should be deleted from my server.

Instead of automatic download, a download link is also fine. but other logic remains same.

like image 379
Joby Joseph Avatar asked Apr 09 '11 08:04

Joby Joseph


People also ask

Can you delete zip files after you extract them?

zip files once they've been extracted, so delete them to recover the storage space. Some installers are delivered as . zip files and must be uncompressed first. On Windows, you can access the files inside while they're still compressed.

How do I delete one file from a Zip file?

Select entries in the main WinZip window, right click on any selected file, and click Delete from the shortcut menu. WinZip will display one of two confirmation windows, depending on which view you are using. Default view: WinZip will ask for confirmation that you want to delete the selected files/folders.


1 Answers

Well, you'll have to first create the zipfile, using the ZipArchive class.

Then, send :

  • The right headers, indicating to the browser it should download something as a zip -- see header() -- there is an example on that manual's page that should help
  • The content of the zip file, using readfile()

And, finally, delete the zip file from your server, using unlink().


Note : as a security precaution, it might be wise to have a PHP script running automatically (by crontab, typically), that would delete the old zip files in your temporary directory.

This just in case your normal PHP script is, sometimes, interrupted, and doesn't delete the temporary file.

like image 87
Pascal MARTIN Avatar answered Sep 20 '22 18:09

Pascal MARTIN