压缩目录中的所有文件并下载生成的.zip

问题描述 投票:28回答:5

好吧,首先,这是我的文件夹结构:

images/

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

这是我的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");

    ?>

如何收集“images /”文件夹中的所有文件,“generate_zip.php”除外,并使其成为可下载的.zip文件?在这种情况下,“images /”文件夹始终具有不同的图像。那可能吗?

php zip directory ziparchive
5个回答
26
投票

这将确保不会添加扩展名为.php的文件:

   foreach ($files as $file) {
        if(!strstr($file,'.php')) $zip->addFile($file);
    }

编辑:这里是重写的完整代码:

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

    ?>

58
投票

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

包括所有子文件夹:

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

首先,包括这个piece of code


1
投票

由于您只需要从目录中创建ZipArchive的特定文件,因此您可以使用glob()函数来执行此操作。

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

如果您尝试列出存储了大量文件的目录(超过100.000)中的文件,请不要使用glob()。您得到“允许的XYZ字节的内存大小耗尽...”错误。

readdir()是更稳定的方式。


0
投票

将你的foreach循环更改为除generate_zip.php之外的其他循环

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

0
投票
<?php 
ob_start();
$zip = new ZipArchive; 
$zip->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`

注意:不要忘记使用ob_start();并结束。

© www.soinside.com 2019 - 2024. All rights reserved.