Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZIP all files in directory and download generated .zip [duplicate]

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?

like image 759
Ygor Montenegro Avatar asked Jul 17 '13 19:07

Ygor Montenegro


People also ask

How do I zip all files in a folder?

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”.

How do I recursive a 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.

How do I zip an entire directory in Linux?

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.

How zip all files in 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'.


2 Answers

======= Working solution !======

includes all sub-folders:

new GoodZipArchive('path/to/input/folder',    'path/to/output_zip_file.zip') ; 

at first, include this piece of code.

like image 138
T.Todua Avatar answered Oct 01 '22 07:10

T.Todua


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");      ?> 
like image 42
skrilled Avatar answered Oct 01 '22 09:10

skrilled