PHP处理大文件下载

 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;