php 檔案限速下載代碼

來源:互聯網
上載者:User

標籤:root   cond   中文不能顯示   緩衝區   pen   cat   判斷   ade   路徑   

<?phpinclude("DBDA.class.php");$db = new DBDA();$bs = $_SERVER["QUERY_STRING"];  //擷取由提交介面傳過來的參數$bss = substr($bs,3);  //截取 = 後面的值$sql = "select video from shangpin where id=‘{$bss}‘";  //擷取視頻檔案路徑$str = $db->StrQuery($sql);$wjm = substr($str,13);  //截取檔案名稱$lj = substr($str,0,13);  //截取檔案所在檔案夾路徑 $file_name = $wjm;   //檔案名稱//用以解決中文不能顯示出來的問題  $file_name=iconv("utf-8","gb2312",$file_name);  $file_sub_path=$_SERVER[‘DOCUMENT_ROOT‘].$lj;   //擷取當前運行指令碼所在的主目錄$file_path=$file_sub_path.$file_name;   //拼成一個完整的檔案所在的路徑// $file = include pathif(file_exists($file_path))  //判斷檔案是否存在{    header(‘Content-Description: File Transfer‘);       //header函數是提交給表頭的是一些下載的規格    header(‘Content-Type: application/octet-stream‘);    header(‘Content-Disposition: attachment; filename=‘.basename($file_path));    header(‘Content-Transfer-Encoding: binary‘);    header(‘Expires: 0‘);    header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0‘);    header(‘Pragma: public‘);    header(‘Content-Length: ‘ . filesize($file_path));    ob_clean();   //ob_clean這個函數的作用就是用來丟棄輸出緩衝區中的內容,如果你的網站有許多產生的檔案,那麼想要訪問正確,就要經常清除緩衝區    flush();   //ob_flush()和flush()的區別。前者是把資料從PHP的緩衝中釋放出來,後者是把不在緩衝中的或者說是被釋放出來的資料發送到瀏覽器。所以當緩衝存在的時候,我們必須ob_flush()和flush()同時使用。        $file = fopen($file_path, "r");  //開啟指定的檔案,r 代表唯讀,如果找不到,返回false    while(!feof($file))  //判斷是否存在    {        // send the current file part to the browser        print fread($file, round(3000 * 1024));  //先頂下載速度為3MB        // flush the content to the browser        flush();  //傳給瀏覽器        // sleep one second        sleep(1);  //等待1秒    }    fclose($file);  //關閉檔案        /*readfile($file_path);  //也可以用這種方法,發送完表頭直接輸出,不過沒有限速    exit;*/}

 

php 檔案限速下載代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.