php 文件限速下载代码
生活随笔
收集整理的這篇文章主要介紹了
php 文件限速下载代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?php include("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 path if(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 代表只讀,如果找不到,返回falsewhile(!feof($file)) //判斷是否存在 {// send the current file part to the browserprint fread($file, round(3000 * 1024)); //先頂下載速度為3MB// flush the content to the browserflush(); //傳給瀏覽器// sleep one secondsleep(1); //等待1秒 }fclose($file); //關閉文件/*readfile($file_path); //也可以用這種方法,發送完表頭直接輸出,不過沒有限速exit;*/ }
?
轉載于:https://www.cnblogs.com/zxl89/p/6130252.html
總結
以上是生活随笔為你收集整理的php 文件限速下载代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9、 Struts2验证(声明式验证、自
- 下一篇: 网页中;window.onerror=f