dpxo13079 2014-02-10 19:50
浏览 71

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

The following PHP snippet is causing an internal 500 error and I believe it's one or more of the headers. I have enabled error reporting but is it reporting nothing. Can someone see what the problem may be? Thanks.

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

Oddly enough, the file is downloaded and I get response headers back:

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

But the MS Word and Excel documents are corrupted. PDF and text files load fine. Have not checked image files.

  • 写回答

1条回答 默认 最新

  • douhuan1380 2014-02-10 20:18
    关注

    Can you check what basename($file) is? Is there any extension attached to it like .pdf. If it's not there then it will result in error. Add .pdf or any format you want to download and try.
    Also you can try below for setting header:

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog