ZIP all files in directory and download generated

2019-01-13 13:18发布

问题:

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?

回答1:

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

    ?>


回答2:

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



回答3:

Since you just need specific files from a directory to create ZipArchive you can use glob() function to do this.

<?php
    $zip = new ZipArchive;
    $download = 'download.zip';
    $zip->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.



回答4:

change your foreach loop to this to except generate_zip.php

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