2014-05-03 51 views
0

我有下面的代碼(從http://www.php.net/manual/en/function.readfile.php)正常投放文件下載:PHP:如何使用ReadFile的與輸出緩衝壓縮

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 

但如果我有ob_start('ob_gzhandler');之前header()聲明它沒有工作。

回答

1

使用此代碼(你不需要修改ob_start('ob_gzhandler');,如果你之前的話):

error_reporting(0); //Errors may corrupt download 
ob_start(); //Insert this 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
//header('Content-Length: ' . filesize($file)); //Content now is compressed, you need to disable this 
//Note: Download also works if you do not comment out Content-Lenght (tested in IE8) 
ob_clean(); 
ob_end_flush(); //Modify flush() to ob_end_flush(); 
readfile($file); 
exit; 
+0

不幸的是,這並沒有爲我工作。至少在一臺特定的服務器上,它導致下載在大約80MB後終止。你可能想看看[在相關問題中的這個答案](https://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click -for-pdf-in-html/19416831#19416831),這對我來說效果很好。 – Hendrik