標籤:
<?php
/* * PHP下載斷點續傳 * from:php100 */function dl_file_resume($file){ //檢測檔案是否存在 if (!is_file($file)) { die("<b>404 File not found!</b>"); } $len = filesize($file);//擷取檔案大小 $filename = basename($file);//擷取檔案名稱字 $file_extension = strtolower(substr(strrchr($filename,"."),1));//擷取副檔名 //根據副檔名 指出輸出瀏覽器格式 switch( $file_extension ) { case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "mp3": $ctype="audio/mpeg"; break; case "mpg":$ctype="video/mpeg"; break; case "avi": $ctype="video/x-msvideo"; break; default: $ctype="application/force-download"; } //Begin writing headers header("Cache-Control:"); header("Cache-Control: public"); //設定輸出瀏覽器格式 header("Content-Type: $ctype"); if (strstr($_SERVER[‘HTTP_USER_AGENT‘], "MSIE")) {//如果是IE瀏覽器 # workaround for IE filename bug with multiple periods / multiple dots in filename # that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe $iefilename = preg_replace(‘/\\./‘, ‘%2e‘, $filename, substr_count($filename, ‘.‘) - 1); header("Content-Disposition: attachment; filename=\\"$iefilename\\""); } else { header("Content-Disposition: attachment; filename=\\"$filename\\""); } header("Accept-Ranges: bytes"); $size=filesize($file); //如果有$_SERVER[‘HTTP_RANGE‘]參數 if(isset($_SERVER[‘HTTP_RANGE‘])) { /* --------------------------- Range頭域 Range頭域可以請求實體的一個或者多個子範圍。例如, 表示頭500個位元組:bytes=0-499 表示第二個500位元組:bytes=500-999 表示最後500個位元組:bytes=-500 表示500位元組以後的範圍:bytes=500- 第一個和最後一個位元組:bytes=0-0,-1 同時指定幾個範圍:bytes=500-600,601-999 但是伺服器可以忽略此要求標頭,如果無條件GET包含Range要求標頭,響應會以狀態代碼206(PartialContent)返回而不是以200 (OK)。 ---------------------------*/ // 斷點後再次串連 $_SERVER[‘HTTP_RANGE‘] 的值 bytes=4390912- list($a, $range)=explode("=",$_SERVER[‘HTTP_RANGE‘]); //if yes, download missing part str_replace($range, "-", $range);//這句幹什麼的呢。。。。 $size2=$size-1;//檔案總位元組數 $new_length=$size2-$range;//擷取下次下載的長度 header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length");//輸入總長 header("Content-Range: bytes $range$size2/$size");//Content-Range: bytes 4908618-4988927/4988928 95%的時候 } else {//第一次串連 $size2=$size-1; header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928 header("Content-Length: ".$size);//輸出總長 } //開啟檔案 $fp=fopen("$file","rb"); //設定指標位置 fseek($fp,$range); //虛幻輸出 while(!feof($fp)){ //設定檔案最長執行時間 set_time_limit(0); print(fread($fp,1024*8));//輸出檔案 flush();//輸出緩衝 ob_flush(); } fclose($fp); exit; } dl_file_resume("1.zip");//同級目錄的1.zip 檔案 //--------------------------------------- //不支援斷點續傳的檔案下載。 //--------------------------------------- downFile("1.zip"); function downFile($sFilePath) { if(file_exists($sFilePath)){ $aFilePath=explode("/",str_replace("\\\\","/",$sFilePath),$sFilePath); $sFileName=$aFilePath[count($aFilePath)-1]; $nFileSize=filesize ($sFilePath); header ("Content-Disposition: attachment; filename=" . $sFileName); header ("Content-Length: " . $nFileSize); header ("Content-type: application/octet-stream"); readfile($sFilePath); } else { echo("檔案不存在!"); } } ?>//該片段來自於http://www.codesnippet.cn/detail/24112012641.html
來源地址: http://www.phpxs.com/code/1009963/
PHP實現檔案下載斷點續傳