Download folder as zip file in php

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:

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?

Download folder as zip file in php

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:

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

at first, include this piece of code.

answered Oct 18, 2013 at 14:17

Download folder as zip file in php

8

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:

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");

    ?>

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.

open($download, ZipArchive::CREATE);
    foreach (glob("images/*.png") as $file) { /* Add appropriate path to read content of zip */
        $zip->addFile($file);
    }
    $zip->close();
    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename = $download");
    header('Content-Length: ' . filesize($download));
    header("Location: $download");
 ?>

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

Download folder as zip file in php

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

Download folder as zip file in php

change your foreach loop to this to except generate_zip.php

 foreach ($files as $file) {
     if($file != "generate_zip.php"){
        $zip->addFile($file);
     }
 }

answered Jul 17, 2013 at 19:45

Download folder as zip file in php

shyammakwana.meshyammakwana.me

5,3242 gold badges26 silver badges49 bronze badges

open('sample.zip',  ZipArchive::CREATE);
$srcDir = "C:\\xampp\\htdocs\\uploads"; //location of the directory
$files= scandir($srcDir);
print_r($files);  // to check if files are actually coming in the array

unset($files[0],$files[1]);
foreach ($files as $file) {
    $zip->addFile($srcDir.'\\'.$file, $file);
    echo "bhandari";
}
$zip->close();

$file='sample.zip';
if (headers_sent()) {
    echo 'HTTP header already sent';
} else {
    if (!is_file($file)) {
        header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
        echo 'File not found';
    } else if (!is_readable($file)) {
        header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
        echo 'File not readable';
    } else {
        header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length: ".filesize($file));
        header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
        while (ob_get_level()) {
            ob_end_clean();
          }
        readfile($file);
        exit;
    }
}



?>`enter code here`

Note: Don't forget to use ob_start(); and end .

Download folder as zip file in php

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);
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.