PHP 文件下载 兼容ie8,ie7,google chrome

header('Content-Description: File Transfer');
header('Cache-Control: private, must-revalidate,post-check=0, pre-check=0, max-age=1');//这句兼容低版本ie


//header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: image/gif');
// use the Content-Disposition header to supply a recommended filename
header('Content-Disposition: attachment; filename='.$saveFileName);
header('Content-Transfer-Encoding: binary');
echo file_get_contents($file);

exit;


-----------------------------------------------------------------------------------大文件下载存在问题,修改成下面的下载方式--------------------------------------


$path =‘data/test.exe’; 
$mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so
$filesize = filesize($path);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .$filesize );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
$chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
if ($filesize > $chunksize) { 
 $handle = fopen($path, 'rb'); 
 $buffer = ''; 
 while (!feof($handle)) { 
$buffer = fread($handle, $chunksize); 
echo $buffer; 
ob_flush(); 
flush(); 
 } 
 fclose($handle); 
} else { 
 readfile($path); 

//readfile($path); // outputs the content of the file


exit();



你可能感兴趣的:(PHP)