下载 .zip 文件会运行损坏的文件 php

发布于 2024-08-18 03:05:04 字数 507 浏览 3 评论 0原文

我正在尝试强制下载受保护的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

阳光下的泡沫是彩色的 2024-08-25 03:05:04

此问题可能有多种原因。也许您的文件未找到或无法读取,因此文件的内容只是 PHP 错误消息。或者 HTTP 标头已经发送。或者您有一些额外的输出,然后会损坏文件的内容。

尝试在脚本中添加一些错误处理,如下所示:

$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;
    }
}

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:

$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;
    }
}
暗恋未遂 2024-08-25 03:05:04

我打赌 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.

廻憶裏菂餘溫 2024-08-25 03:05:04

迟到的答案,但对于无法强制下载工作的用户来说可能有用。
php 脚本顶部添加以下内容

<?php
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);

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

<?php
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
笑看君怀她人 2024-08-25 03:05:04

对于大型 zip 文件,我也遇到了类似的问题
这对我有用:

在你的 php.ini 中,更改为:

  • Upload_max_filesize - 1500 M
  • Max_input_time - 1000
  • Memory_limit - 640M
  • Max_execution_time - 1800
  • Post_max_size - 2000 M

在你的 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 ();

I had similar problems for large zip files
This works for me:

In your php.ini, change to:

  • Upload_max_filesize - 1500 M
  • Max_input_time - 1000
  • Memory_limit - 640M
  • Max_execution_time - 1800
  • Post_max_size - 2000 M

In your php file.

    $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 ();
无人问我粥可暖 2024-08-25 03:05:04

这是解决方案
创建一个名为 .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

以酷 2024-08-25 03:05:04

尝试这个来查找是否有任何错误

error_reporting(0);

在写入标题之前不要打印任何内容;
在代码编辑标题后运行 ob_start() 到您的脚本,然后运行 ​​ob_flush() 和 ob_clean()

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()

谷夏 2024-08-25 03:05:04

@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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文