PHP标头功能导致内部500错误

时间:2014-02-10 19:50:19

标签: php file download

以下PHP代码段导致内部500错误,我相信它是一个或多个标头。我已启用错误报告,但它没有报告任何内容。有人能看出问题可能是什么吗?感谢。

$file = CLIENTFOLDERS.$_GET['download'];
$fileSize = filesize($file);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $file);
finfo_close($finfo);        
header("Cache-Control: private");
header("Content-Type: ".$type);
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".basename($file));       
readfile($file);                
exit();

奇怪的是,文件已下载,我收到了回复标题:

Cache-Control:private
Connection:Keep-Alive
Content-Disposition:attachment; filename=CPS Letterhead form.doc
Content-Length:4
Content-Type:application/msword
Date:Mon, 10 Feb 2014 20:21:00 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.7 (Win32) PHP/5.5.6
X-Powered-By:PHP/5.5.6

但MS Word和Excel文档已损坏。 PDF和文本文件加载正常。没有检查图像文件。

1 个答案:

答案 0 :(得分:0)

你能检查一下basename($ file)是什么吗?是否有像.pdf这样的附加扩展名。如果它不存在那么它将导致错误。添加.pdf或您要下载的任何格式并尝试 您也可以在下面尝试设置标题:

header("Pragma: public"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"basename($file)\"");
header("Content-Type: ".$type);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fileSize);