標籤:
1 <?php 2 //對函數的說明 3 //參數說明 $file_name 檔案名稱 4 // $file_sub_dir: 下載檔案的子路徑 ‘"/xxx/xxx/" 5 function down_file($file_name,$file_sub_dir){ 6 //死去活來,示範下載一個圖片. 7 //如果檔案是中文. 8 9 10 //原因 php檔案函數,比較古老,需要對中文轉碼 gb231211 $file_name=iconv("utf-8","gb2312",$file_name);12 //絕對路徑13 $file_path=$_SERVER[‘DOCUMENT_ROOT‘].$file_sub_dir.$file_name;14 //如果你希望絕對路徑15 16 //1.開啟檔案17 echo $file_path;18 if(!file_exists($file_path)){19 echo "檔案不存在!";20 return ;21 }22 $fp=fopen($file_path,"r");23 24 //擷取下載檔案的大小25 $file_size=filesize($file_path);26 // if($file_size>30){27 28 // echo "<script language=‘javascript‘>window.alert(‘過大‘)</script>";29 // return ;30 // }31 //返回的檔案32 header("Content-type: application/octet-stream");33 //按照位元組大小返回34 header("Accept-Ranges: bytes");35 //返迴文件大小36 header("Accept-Length: $file_size");37 //這裡用戶端的彈出對話方塊,對應的檔案名稱38 header("Content-Disposition: attachment; filename=".$file_name);39 //向用戶端回送資料40 41 $buffer=1024;42 //為了下載的安全,我們最好做一個檔案位元組讀取計數器43 $file_count=0;44 //這句話用於判斷檔案是否結束45 while(!feof($fp) && ($file_size-$file_count>0) ){46 $file_data=fread($fp,$buffer);47 //統計讀了多少個位元組48 $file_count+=$buffer;49 //把部分資料回送給瀏覽器;50 echo $file_data;51 }52 //關閉檔案53 fclose($fp);54 }55 //測試函數是否可用56 down_file("Day by Day 完整版.mp4","/html5/");57 ?>
php檔案下載