php 创建一个 zip 文件并下载它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12225964/
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
Create a zip file and download it
提问by Harshal Mahajan
I am trying to download a 2 files by creating the zip file on local-server.the file is downloaded in zip format but when i try to extract it.it gives error: End-of-central-directory signature not found. Either this file is not a zip file, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zip file comment will be found on the last disk(s) of this archive.
我试图通过在本地服务器上创建 zip 文件来下载 2 个文件。该文件以 zip 格式下载,但是当我尝试提取它时。它给出错误: 找不到中央目录结尾签名。该文件要么不是 zip 文件,要么构成了多部分存档的一个磁盘。在后一种情况下,将在此存档的最后一个磁盘上找到中央目录和 zip 文件注释。
the following code i am using for this:
我为此使用了以下代码:
<?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
//Archive name
$archive_file_name=$name.'iMUST_Products.zip';
//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
//echo $file_path;die;
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
//echo $file_path.$files,$files."
}
$zip->close();
//then send the headers to force download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
?>
i checked the values of all variables which are passing into the function,all are fine.so please look this.Thanks in advance.
我检查了传递给函数的所有变量的值,一切都很好。所以请看这个。提前致谢。
回答by Kuba Wyrostek
Add Content-lengthheader describing size of zip file in bytes.
添加Content-length以字节为单位描述 zip 文件大小的标头。
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
Also make sure that there is absolutely no white space before <?and after ?>. I see a space here:
另外,还要确保绝对不会有空格之前<?和之后?>。我在这里看到一个空格:
↓
↓
<?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
回答by M Arfan
$zip = new ZipArchive;
$tmp_file = 'assets/myzip.zip';
if ($zip->open($tmp_file, ZipArchive::CREATE)) {
$zip->addFile('folder/bootstrap.js', 'bootstrap.js');
$zip->addFile('folder/bootstrap.min.js', 'bootstrap.min.js');
$zip->close();
echo 'Archive created!';
header('Content-disposition: attachment; filename=files.zip');
header('Content-type: application/zip');
readfile($tmp_file);
} else {
echo 'Failed!';
}
回答by vitams
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);
I found this soludtion hereand it work for me
我在这里找到了这个解决方案,它对我有用
回答by David Rodriguez
I just ran into this problem. For me the issue was with:
我刚刚遇到了这个问题。对我来说,问题在于:
readfile("$archive_file_name");
It was resulting in a out of memory error.
它导致了内存不足错误。
Allowed memory size of 134217728 bytes exhausted (tried to allocate 292982784 bytes)
I was able to correct the problem by replacing readfile() with the following:
我能够通过将 readfile() 替换为以下内容来纠正问题:
$handle = fopen($zipPath, "rb");
while (!feof($handle)){
echo fread($handle, 8192);
}
fclose($handle);
Not sure if this is your same issue or not seeing that your file is only 1.2 MB. Maybe this will help someone else with a similar problem.
不确定这是否与您的问题相同,或者没有看到您的文件只有 1.2 MB。也许这会帮助其他有类似问题的人。
回答by Avinav
One of the error could be that the file is not read as 'archive' format. check out ZipArchive not opening file - Error Code: 19. Open the downloaded file in text editor, if you have any html tags or debug statements at the starting, clear the buffer before reading the file.
错误之一可能是文件没有被读取为“存档”格式。检查ZipArchive 未打开文件 - 错误代码:19。在文本编辑器中打开下载的文件,如果开头有任何html标签或调试语句,请在读取文件之前清除缓冲区。
ob_clean();
flush();
readfile("$archive_file_name");
回答by user2752793
I have experienced exactly the same problem. In my case, the source of it was the permissions of the folder in which I wanted to create the zip file that were all set to read only. I changed it to read and write and it worked.
我遇到了完全相同的问题。就我而言,它的来源是我想在其中创建所有设置为只读的 zip 文件的文件夹的权限。我将其更改为读写,并且有效。
If the file is not created on your local-server when you run the script, you most probably have the same problem as I did.
如果运行脚本时该文件未在本地服务器上创建,则您很可能遇到与我相同的问题。
回答by LSerni
but the file i am getting from server after download it gives the size of 226 bytes
但是我下载后从服务器获取的文件大小为 226 字节
This is the size of a ZIP header. Apparently there is no datain the downloaded ZIP file. So, can you verify that the files to be added into the ZIP file are, indeed, there (relative to the path of the download PHP script)?
这是 ZIP 标头的大小。显然,下载的 ZIP 文件中没有数据。那么,您能否验证要添加到 ZIP 文件中的文件是否确实存在(相对于下载 PHP 脚本的路径)?
Consider adding a check on addFiletoo:
也考虑添加一个检查addFile:
foreach($file_names as $file)
{
$inputFile = $file_path . $file;
if (!file_exists($inputFile))
trigger_error("The input file $inputFile does not exist", E_USER_ERROR);
if (!is_readable($inputFile))
trigger_error("The input file $inputFile exists, but has wrong permissions or ownership", E_USER_ERROR);
if (!$zip->addFile($inputFile, $file))
trigger_error("Could not add $inputFile to ZIP file", E_USER_ERROR);
}
The observed behaviour is consistent with some problem (path error, permission problems, ...) preventing the files from being added to the ZIP file. On receiving an "empty" ZIP file, the client issues an error referring to the ZIP central directory missing (the actual error being that there is no directory, and no files).
观察到的行为与阻止将文件添加到 ZIP 文件的某些问题(路径错误、权限问题等)一致。在收到“空”ZIP 文件时,客户端会发出一个错误,指出 ZIP 中央目录丢失(实际错误是没有目录,也没有文件)。

