1. 程式人生 > >PHP處理大文件下載

PHP處理大文件下載

length files ext 下載文件 head ech IT 緩沖 false

 1 <?php
 2 set_time_limit(0);  //大文件在讀取內容未結束時會被超時處理,導致下載文件不全。  
 3   
 4 $fpath = ‘book.zip‘;
 5 $file_pathinfo = pathinfo($fpath);  
 6 $file_name = $file_pathinfo[‘basename‘];  
 7 $file_extension = $file_pathinfo[‘extension‘];  
 8 $handle = fopen($fpath,"rb");  
 9 if (FALSE === $handle)  
10     exit
("Failed to open the file"); 11 $filesize = filesize($fpath); 12 13 header("Content-type:video/mpeg4");//更具不同的文件類型設置header輸出類型 14 header("Accept-Ranges:bytes"); 15 header("Accept-Length:".$filesize); 16 header("Content-Disposition: attachment; filename=".$file_name); 17 18 $contents = ‘‘;
19 while (!feof($handle)) { 20 $contents = fread($handle, 8192); 21 echo $contents; 22 @ob_flush(); //把數據從PHP的緩沖中釋放出來 23 flush(); //把被釋放出來的數據發送到瀏覽器 24 } 25 fclose($handle); 26 exit;

PHP處理大文件下載