.zip文件的下载运行损坏的文件php [英] Download of .zip file runs a corrupted file php

查看:136
本文介绍了.zip文件的下载运行损坏的文件php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图强制下载受保护的zip文件(我不希望人们无需登录就可以访问。



我有这个功能为登录创建的等等,但我遇到一个问题,下载的文件被破坏。



以下是我的代码:

  $ file ='.. / downloads /'.$ filename; 
header 内容类型:application / zip; \\\
);
头(Content-Transfer-Encoding:Binary);
头(Content-length:.filesize($ file) 。; \\\
);
header(Content-disposition:attachment; filename = \。basename($ file)。\);
readfile文件);
exit();

这是错误:无法打开文件:它似乎不是一个有效的归档。



该文件下载不好,所以它必须是我' m / p>

任何想法?

解决方案

此问题可能有几个原因。也许您的文件未找到或无法读取,因此该文件的内容只是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);
退出;
}
}


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?

解决方案

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

这篇关于.zip文件的下载运行损坏的文件php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆