我有以下代码(来自http://www.php.net/manual/en/function.readfile.php):
)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()
语句。
使用以下代码(如果您之前有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;