PHP强制下载导致文件损坏

时间:2013-07-14 21:42:01

标签: php download php-safe-mode

我有一个强制下载的PHP脚本。继承我的代码

//$file and $mime have been set earlier
$basename = basename($file);
$length   = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: Binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);

现在这个脚本在我的本地服务器上完美运行,但是当我将它上传到我的网站并尝试出来时,下载了文件(为了测试它我使用的是图像),但是当我打开它时,我得到了{{ 1}}

我在Sublime Text中打开了文件,这就是它的内容
警告:set_time_limit()[function.set-time-limit]:无法在 / mounted-storage / home61c / sub001 / sc38639-USWQ / www / test /的安全模式下设置时间限制脚本 32 上的scripts / download.php ‰PNG

bla bla bla(我无法复制并粘贴下面的东西)

发生了什么事?

3 个答案:

答案 0 :(得分:1)

你可能在输出缓冲区中有不需要的东西。

包含

flush();
ob_clean();

在开始下载之前,只是为了确保一切都干净。

答案 1 :(得分:1)

set_time_limit在安全模式下没有任何效果,并且该警告会滑入您的输出,从而破坏它。 请参阅http://www.php.net/manual/en/features.safe-mode.functions.phphttp://php.net/manual/en/features.safe-mode.php

尝试关闭安全模式或不使用set_time_limit并尝试在php.ini中扩展默认运行时间,请参阅http://www.php.net/manual/en/function.set-time-limit.php

答案 2 :(得分:1)

您有两个选择:

评论此行:

set_time_limit(0);

禁用安全模式。

相关问题