简体   繁体   中英

PHP Header Function causing internal 500 error

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM