php 压缩目录中的所有文件并下载生成的 .zip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17708562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ZIP all files in directory and download generated .zip
提问by Ygor Montenegro
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:
这是我的 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?
如何从“images/”文件夹中收集所有文件,“generate_zip.php”除外,并使其成为可下载的 .zip?在这种情况下,“images/”文件夹总是有不同的图像。那可能吗?
采纳答案by skrilled
this will ensure a file with .php extension will not be added:
这将确保不会添加扩展名为 .php 的文件:
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");
?>
回答by T.Todua
======= 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.
首先,包含这段代码。
回答by way2vin
Since you just need specific files from a directory to create ZipArchive you can use glob()function to do this.
由于您只需要目录中的特定文件来创建 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");
?>
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.
如果您尝试在存储了大量文件(超过 100.000)的目录中列出文件,请不要使用 glob()。您会收到“已用尽 XYZ 字节的允许内存大小...”错误。
readdir()is more stable way.
readdir()是更稳定的方式。
回答by shyammakwana.me
change your foreach loop to this to except generate_zip.php
将您的 foreach 循环更改为此到 except generate_zip.php
foreach ($files as $file) {
if($file != "generate_zip.php"){
$zip->addFile($file);
}
}
回答by sawan
<?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`
Note: Don't forget to use ob_start(); and end .
注意:不要忘记使用 ob_start(); 并结束。
回答by Marchand
I went with way2vin's solution. However I had to replace
我采用了way2vin的解决方案。但是我不得不更换
$zip->addFile("{$file}");
$zip->addFile("{$file}");
with
和
$zip->addFromString(basename($file), file_get_contents($file));
$zip->addFromString(basename($file), file_get_contents($file));
which did the trick. Found this here: Creating .zip file
成功了。在这里找到:创建 .zip 文件