Download folder as zip file in php
Well, first of all, this is my folder structure: Show
And this is mine generate_zip.php:
How to gather all the files from "images/" folder, except "generate_zip.php", and make it a downloadable .zip? In this case the "images/" folder always have a different image. Is that possible?
T.Todua 50.1k19 gold badges217 silver badges213 bronze badges asked Jul 17, 2013 at 19:26
Ygor MontenegroYgor Montenegro 6841 gold badge12 silver badges26 bronze badges 0 ======= Working solution !======includes all sub-folders:
at first, include this piece of code. answered Oct 18, 2013 at 14:17
8 this will ensure a file with .php extension will not be added:
edit: here's the full code rewritten:
answered Jul 17, 2013 at 19:28
skrilledskrilled 5,2522 gold badges23 silver badges47 bronze badges 9 Since you just need specific files from a directory to create ZipArchive you can use glob() function to do this.
Don't use glob() if you try to list files in a directory where very much files are stored (more than 100.000). You get an "Allowed memory size of XYZ bytes exhausted ..." error. readdir() is more stable way. answered Jul 17, 2013 at 19:53
way2vinway2vin 2,3211 gold badge20 silver badges15 bronze badges I went with way2vin's solution. However I had to replace $zip->addFile("{$file}"); with $zip->addFromString(basename($file), file_get_contents($file)); which did the trick. Found this here: Creating .zip file answered Dec 26, 2019 at 11:55
change your foreach loop to this to except
answered Jul 17, 2013 at 19:45
shyammakwana.meshyammakwana.me 5,3242 gold badges26 silver badges49 bronze badges
Note: Don't forget to use ob_start(); and end .
crenshaw-dev 6,6443 gold badges41 silver badges72 bronze badges answered Apr 23, 2019 at 19:41
sawansawan 32 bronze badges How to zip a folder and download in php?php $files = array($listfiles); $zipname = 'adcs. zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); header('Content-Type: application/zip'); header("Content-Disposition: attachment; filename='adcs.
How can I zip a folder in php?Here we use XAMPP to run a local web server. Place the php files along with the directory to be zipped in C:\xampp\htdocs(XAMPP is installed in C: drive in this case). In the browser, enter https://localhost/zip.php as the url and the file will be zipped. After this a new zip file is created named 'file'.
How to download file in zip php?$download_file = file_get_contents( $file_url ); $zip->addFromString(basename($file_url),$download_file);
How to create a download link for a zip file in php?If the reading value is the file then add it to zip object using addFile() method. If the value is a directory then create an empty directory and call createZip() function where pass the directory path. Check if the zip file exists or not. If it exists then download and remove it from the server.
|