国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > php教程 > PHP源碼 > 正文

PHP實現(xiàn)文件下載斷點續(xù)傳

大家講道理
發(fā)布: 2016-11-08 11:21:45
原創(chuàng)
1653人瀏覽過

<?php 
/*
 * PHP下載斷點續(xù)傳
 *
 */
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個字節(jié):bytes=0-499   表示第二個500字節(jié):bytes=500-999   表示最后500個字節(jié):bytes=-500   表示500字節(jié)以后的范圍:bytes=500-   第一個和最后一個字節(jié):bytes=0-0,-1   同時指定幾個范圍:bytes=500-600,601-999   但是服務器可以忽略此請求頭,如果無條件GET包含Range請求頭,響應會以狀態(tài)碼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;//文件總字節(jié)數 
        $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)){ 
        //設置文件最長執(zhí)行時間 
        set_time_limit(0); 
        print(fread($fp,1024*8));//輸出文件 
        flush();//輸出緩沖 
        ob_flush(); 
    } 
    fclose($fp); 
    exit; 
} 
 
 
dl_file_resume("1.zip");//同級目錄的1.zip 文件 
 
 
 
 
//---------------------------------------
 
//不支持斷點續(xù)傳的文件下載。 
 
//---------------------------------------
 
   
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
登錄后復制

PHP速學教程(入門到精通)
PHP速學教程(入門到精通)

PHP怎么學習?PHP怎么入門?PHP在哪學?PHP怎么學才快?不用擔心,這里為大家提供了PHP速學教程(入門到精通),有需要的小伙伴保存下載就能學習啦!

下載
來源:php中文網
本文內容由網友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內容,請聯(lián)系admin@php.cn
作者最新文章
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網:公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網訂閱號
每天精選資源文章推送
PHP中文網APP
隨時隨地碎片化學習
PHP中文網抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號