ZIP all files in directory and download generated .zip

后端 未结 6 1591
逝去的感伤
逝去的感伤 2020-12-04 18:12

Well, first of all, this is my folder structure:

images/

image1.png
image11.png
image111.png
image223.png
generate_zip.php

And this is min

6条回答
  •  伪装坚强ぢ
    2020-12-04 18:42

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

提交回复
热议问题