下载 .zip 文件运行损坏的文件 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2088267/
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
Download of .zip file runs a corrupted file php
提问by Troy
I'm trying to force a download of a protected zip file (I don't want people to access it without logging in first.
我正在尝试强制下载受保护的 zip 文件(我不希望人们在未先登录的情况下访问它。
I have the function created for the loginand such , but I'm running into a problem where the downloaded file is corrupting.
我有为login等创建的函数,但我遇到了下载文件损坏的问题。
Here's the code I have:
这是我的代码:
$file='../downloads/'.$filename;
header("Content-type: application/zip;\n");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file).";\n");
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile("$file");
exit();
Here's the error: Cannot open file: It does not appear to be a valid archive.
这是错误: Cannot open file: It does not appear to be a valid archive.
The file downloads fine otherwise, so it must be something I'm doing wrong with the headers.
否则文件下载正常,所以它一定是我在标题上做错了。
Any ideas?
有任何想法吗?
回答by Gumbo
This issue can have several causes. Maybe your file is not found or it can not be read and thus the file's content is just the PHP error message. Or the HTTP header is already sent. Or you have some additional output that then corrupts your file's content.
此问题可能有多种原因。也许您的文件未找到或无法读取,因此文件的内容只是 PHP 错误消息。或者 HTTP 标头已经发送。或者您有一些额外的输出会破坏您的文件内容。
Try to add some error handling into your script like this:
尝试在脚本中添加一些错误处理,如下所示:
$file='../downloads/'.$filename;
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)."\"");
readfile($file);
exit;
}
}
回答by Pekka
I bet two beers that a PHP error occurs, and the error message messes up the ZIP file. The requested file probably doesn't exist.
我打赌两杯啤酒会发生PHP错误,错误信息弄乱了ZIP文件。请求的文件可能不存在。
Open the ZIP file with notepad or a similar text editor, and find out what's wrong.
用记事本或类似的文本编辑器打开 ZIP 文件,找出问题所在。
回答by abobjects.com
Here is the solution create a file with name .htaccess write below line in that SetEnv no-gzip dont-vary
这是在 SetEnv no-gzip dont-vary 中创建一个名称为 .htaccess 的文件的解决方案
upload the file to your website. If you have the file already there then please make the above change in that
将文件上传到您的网站。如果您已经有该文件,请在该文件中进行上述更改
回答by CONvid19
Late answer but maybe useful for users not being able to make force downloadwork.
Add the following at the top of your phpscript
迟到的答案,但可能对无法force download工作的用户有用。
在php脚本顶部添加以下内容
<?php
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
回答by borgomeister
I had similar problems for large zip files This works for me:
我对大型 zip 文件有类似的问题这对我有用:
In your php.ini, change to:
在您的 php.ini 中,更改为:
- Upload_max_filesize - 1500 M
- Max_input_time - 1000
- Memory_limit - 640M
- Max_execution_time - 1800
- Post_max_size - 2000 M
- Upload_max_filesize - 1500 M
- Max_input_time - 1000
- 内存限制 - 640M
- Max_execution_time - 1800
- Post_max_size - 2000 M
In your php file.
在你的 php 文件中。
$filename = "MyFile.zip";
$filepath='../downloads/'.$filename; //your folder file path
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($filepath));
header("Content-Disposition:attachment;filename=\"".basename($filepath)."\"");
while (ob_get_level())
{
ob_end_clean();
}
readfile($filepath);
exit;
ob_start ();
回答by Shuhad zaman
try this to find if there is any error
试试这个,看看是否有任何错误
error_reporting(0);
Do not print anything before writing the headers; Run an ob_start() to of your script, after the code edit your headers and then ob_flush() and ob_clean()
在写入标题之前不要打印任何内容;运行 ob_start() 到您的脚本,在代码编辑您的标题之后,然后 ob_flush() 和 ob_clean()

