下载 .zip 文件会运行损坏的文件 php
我正在尝试强制下载受保护的 zip 文件(我不希望人们在没有先登录的情况下访问它。
我为 login
创建了函数等,但我遇到下载文件损坏的问题。
这是我的代码:
$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();
这是错误:无法打开文件:它似乎不是有效的存档。
否则该文件下载正常,因此它一定是我对标题做错了什么
?
I'm trying to force a download of a protected zip file (I don't want people to access it without logging in first.
I have the function created for the login
and such , but I'm running into a problem where the downloaded file is corrupting.
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.
The file downloads fine otherwise, so it must be something I'm doing wrong with the headers.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
此问题可能有多种原因。也许您的文件未找到或无法读取,因此文件的内容只是 PHP 错误消息。或者 HTTP 标头已经发送。或者您有一些额外的输出,然后会损坏文件的内容。
尝试在脚本中添加一些错误处理,如下所示:
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.
Try to add some error handling into your script like this:
我打赌 PHP 会发生错误,并且错误消息会弄乱 ZIP 文件。请求的文件可能不存在。
使用记事本或类似的文本编辑器打开 ZIP 文件,并找出问题所在。
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.
Open the ZIP file with notepad or a similar text editor, and find out what's wrong.
迟到的答案,但对于无法
强制下载
工作的用户来说可能有用。在
php
脚本顶部添加以下内容Late answer but maybe useful for users not being able to make
force download
work.Add the following at the top of your
php
script对于大型 zip 文件,我也遇到了类似的问题
这对我有用:
在你的 php.ini 中,更改为:
在你的 php 文件中。
I had similar problems for large zip files
This works for me:
In your php.ini, change to:
In your php file.
这是解决方案
创建一个名为 .htaccess 的文件
在下面的行中写下
SetEnv no-gzip dont-vary
将文件上传到您的网站。如果您已有该文件,请在其中进行上述更改
Here is the solution
create a file with name .htaccess
write below line in that
SetEnv no-gzip dont-vary
upload the file to your website. If you have the file already there then please make the above change in that
尝试这个来查找是否有任何错误
在写入标题之前不要打印任何内容;
在代码编辑标题后运行 ob_start() 到您的脚本,然后运行 ob_flush() 和 ob_clean()
try this to find if there is any error
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()
@Pekka 赢得 2 瓶啤酒! https://stackoverflow.com/a/2088288/9294174
在我的例子中,打开终端并使用 nano 读取文件向我显示了一条 xdebug 消息......
@Pekka wins 2 beer! https://stackoverflow.com/a/2088288/9294174
In my case opening terminal and reading the file using nano shown me an xdebug message...