php大文件下载处理
set_time_limit(0);ini_set(max_execution_time,0);//get the http get values and set them as php variables//download the recordingsif ($_GET[<span style="color: green;">a</span>] == <span style="color: green;">"download"</span>) {session_cache_limiter(<span style="color: green;">public</span>);<span style="color: green;">if</span> ($_GET[type] = "rec") {$file_path = $_SESSIONswitch[dir]./.base64_decode($_GET[<span style="color: green;">filename</span>]);$filesize = filesize($file_path);<span style="color: green;">if</span> (file_exists($file_path)) {// $fd = fopen($file_path, "rb");if ($_GET[<span style="color: green;">t</span>] == <span style="color: green;">"bin"</span>) {<span style="color: green;">// header("Content-Type: application/force-download");</span>header(<span style="color: green;">"Content-Type: application/octet-stream"</span>);<span style="color: green;">// header("Content-Type: application/download");</span><span style="color: green;">// header("Content-Description: File Transfer");</span>header(<span style="color: green;">Content-Disposition: attachment; filename="</span>.basename($file_path).");}else {$file_ext = substr(base64_decode($_GET[filename]), -3);if ($file_ext == <span style="color: green;">"wav"</span>) {header(<span style="color: green;">"Content-Type: audio/x-wav"</span>);}<span style="color: green;">if</span> ($file_ext == "mp3") {header("Content-Type: audio/mp3");}if ($file_ext == <span style="color: green;">"avi"</span>) {header(<span style="color: green;">"Content-Type: video/avi"</span>);}<span style="color: green;">if</span> ($file_ext == "mp4") {header("Content-Type: video/mp4");}}// header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1// header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the pastheader("Content-Length: " . $filesize);ob_clean();<span style="color: green;">// fpassthru($fd);//针对大文件,规定每次读取文件的字节数为4096字节,直接输出数据$read_buffer = <span style="color: green;">4096</span>;$handle = fopen($file_path, <span style="color: green;">rb</span>);<span style="color: green;">//总的缓冲的字节数</span>$sum_buffer = 0;//只要没到文件尾,就一直读取while(!feof($handle) && $sum_buffer<$filesize) {<span style="color: green;">echo</span> fread($handle,$read_buffer);$sum_buffer += $read_buffer;}<span style="color: green;">//关闭句柄</span>fclose($handle);}}exit;}
本文系作者 @河马 原创发布在河马博客站点。未经许可,禁止转载。
暂无评论数据