php:如何使用带有输出缓冲压缩的readfile正确地服务文件下载

php:如何使用带有输出缓冲压缩的readfile正确地服务文件下载,php,download,output-buffering,http-compression,Php,Download,Output Buffering,Http Compression,我有以下代码(来自): 但是如果我有ob_start('ob_gzhandler'),它就不起作用在header()语句之前。使用此代码(如果以前有ob\u start('ob\u gzhandler');: 不幸的是,这对我不起作用。至少在一台特定的服务器上,它导致下载在大约80MB后终止。你可能想看看,这对我很有效。 header('Content-Description: File Transfer'); header('Content-Type: application/octet-st

我有以下代码(来自):

但是如果我有
ob_start('ob_gzhandler'),它就不起作用
header()
语句之前。

使用此代码(如果以前有
ob\u start('ob\u gzhandler');


不幸的是,这对我不起作用。至少在一台特定的服务器上,它导致下载在大约80MB后终止。你可能想看看,这对我很有效。
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;
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;