Well, first of all, this is my folder structure:
images/ image1.png image11.png image111.png image223.png generate_zip.php
And this is mine generate_zip.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.zip'"); header('Content-Length: ' . filesize($zipname)); header("Location: adcs.zip"); ?>
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?
Right-click on the file or folder.Select “Compressed (zipped) folder”. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.
-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.
The easiest way to zip a folder on Linux is to use the “zip” command with the “-r” option and specify the file of your archive as well as the folders to be added to your zip file. You can also specify multiple folders if you want to have multiple directories compressed in your zip file.
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'.
includes all sub-folders:
new GoodZipArchive('path/to/input/folder', 'path/to/output_zip_file.zip') ;
at first, include this piece of code.
this will ensure a file with .php extension will not be added:
foreach ($files as $file) { if(!strstr($file,'.php')) $zip->addFile($file); }
edit: here's the full code rewritten:
<?php $zipname = 'adcs.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) { $zip->addFile($entry); } } closedir($handle); } $zip->close(); header('Content-Type: application/zip'); header("Content-Disposition: attachment; filename='adcs.zip'"); header('Content-Length: ' . filesize($zipname)); header("Location: adcs.zip"); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With