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.
Enable your php_curl extension; (php.ini),Then use the below code to create the zip. create a folder class and use the code given below:
create a folder class and use the code given below:
I went there looking for a similar solution, and after reading the comments found this turnover : before creating your zip file in a dedicated folder (here called 'zip_files', delete all zip you estimate being older than a reasonable time (I took 24h) :
By doing so, after 24h you only have the last zips created, user by user. Nothing prevents you for doing a clean by cron task sometimes, but the problem with the cron task is if someone is using the zip archive when the cron is executed it will lead to an error. Here the only possible error is if someone waits 24h to DL the archive.
Here's how I've been able to do it in the past. This code assumes you've written the files to a path specified by the
$path
variable. You might have to deal with some permissions issues on your server configuration with using php'sexec
Firstly, you download images from webiste
then, with the files you have downloaded you creatae zipfile (great tute)
finally you sent this zip file to browser using readfile and headers (see Example 1)
Well, you'll have to first create the zipfile, using the
ZipArchive
class.Then, send :
header()
-- there is an example on that manual's page that should helpreadfile()
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.