一個簡單的php檔案下載原始碼,雖不支援斷點續傳等,但是可以滿足一些常用的需求了。php下載檔案其實用一個a標籤就能實現,比如 <a href="web/magento-1.8.1.0.zip">magento-1.8.1.0.zip</a> 。但是遇到一些瀏覽器能識別的格式,比如.txt,.html,.pdf等,再用<a href="web/abc.txt">abc.txt</a> 想必也知道會發生什麼了。
複製代碼 代碼如下:
<?php
/**
* 檔案下載
*
**/
header("Content-type:text/html;charset=utf-8");
download('web/magento-1.8.1.0.zip', 'magento下載');
function download($file, $down_name){
$suffix = substr($file,strrpos($file,'.')); //擷取檔案尾碼
$down_name = $down_name.$suffix; //新檔案名稱,就是下載後的名字
//判斷給定的檔案存在與否
if(!file_exists($file)){
die("您要下載的檔案已不存在,可能是被刪除");
}
$fp = fopen($file,"r");
$file_size = filesize($file);
//下載檔案需要用到的頭
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length:".$file_size);
header("Content-Disposition: attachment; filename=".$down_name);
$buffer = 1024;
$file_count = 0;
//向瀏覽器返回資料
while(!feof($fp) && $file_count < $file_size){
$file_con = fread($fp,$buffer);
$file_count += $buffer;
echo $file_con;
}
fclose($fp);
}
?>
也可以看看這個注釋比較詳細的代碼:
複製代碼 代碼如下:
<?php
//檔案下載,下載一張圖片
//$file_name="Angel.mp3";
$file_name="bjnihao.jpg"; //出現中文 程式無法完成下載 提示:檔案不存在
//對檔案進行轉碼(PHP檔案函數 比較古老 需對中文碼轉成 gb2312)
//iconv — Convert string to requested character encoding
//by www.jb51.net
$file_name=iconv("utf-8","gb2312",$file_name);
//設定檔案下載路徑(相對路徑)
//$file_path="./dowm/".$file_name;
//使用絕對路徑
$file_path=$_SERVER['DOCUMENT_ROOT']."/http/dowm/".$file_name;
//開啟檔案---先判斷再操作
if(!file_exists($file_path)){
echo "檔案不存在";
return ; //直接退出
}
//存在--開啟檔案
$fp=fopen($file_path,"r");
//擷取檔案大小
$file_size=filesize($file_path);
//http 下載需要的回應標頭
header("Content-type: application/octet-stream"); //返回的檔案
header("Accept-Ranges: bytes"); //按照位元組大小返回
header("Accept-Length: $file_size"); //返迴文件大小
header("Content-Disposition: attachment; filename=".$file_name);//這裡用戶端的彈出對話方塊,對應的檔案名稱
//向用戶端返回資料
//設定大小輸出
$buffer=1024;
//為了下載安全,我們最好做一個檔案位元組讀取計數器
$file_count=0;
//判斷檔案指標是否到了檔案結束的位置(讀取檔案是否結束)
while(!feof($fp) && ($file_size-$file_count)>0){
$file_data=fread($fp,$buffer);
//統計讀取多少個位元組數
$file_count+=$buffer;
//把部分資料返回給瀏覽器
echo $file_data;
}
//關閉檔案
fclose($fp);
?>
封裝函數:
<?php
/*
封裝函數:
參數說明----$file_name:檔案名稱
$file_sub_dir:檔案下載的子路徑
*/
function file_dowm($file_name,$file_sub_dir){
//檔案轉碼
$file_name=iconv("utf-8","gb2312",$file_name);
//使用絕對路徑
$file_path=$_SERVER['DOCUMENT_ROOT']."$file_sub_dir".$file_name;
//開啟檔案---先判斷再操作
if(!file_exists($file_path)){
echo "檔案不存在";
return ; //直接退出
}
//存在--開啟檔案
$fp=fopen($file_path,"r");
//擷取檔案大小
$file_size=filesize($file_path);
/*
//這裡可以設定超過多大不能下載
if($file_size>50){
echo "檔案太大不能下載";
return ;
}*/
//http 下載需要的回應標頭
header("Content-type: application/octet-stream"); //返回的檔案
header("Accept-Ranges: bytes"); //按照位元組大小返回
header("Accept-Length: $file_size"); //返迴文件大小
header("Content-Disposition: attachment; filename=".$file_name);//這裡用戶端的彈出對話方塊,對應的檔案名稱
//向用戶端返回資料
//設定大小輸出
$buffer=1024;
//為了下載安全,我們最好做一個檔案位元組讀取計數器
$file_count=0;
//判斷檔案指標是否到了檔案結束的位置(讀取檔案是否結束)
while(!feof($fp) && ($file_size-$file_count)>0){
$file_data=fread($fp,$buffer);
//統計讀取多少個位元組數
$file_count+=$buffer;
//把部分資料返回給瀏覽器
echo $file_data;
}
//關閉檔案
fclose($fp);
}
file_dowm("bjnihao.jpg","/http/dowm/");
?>
另一個代碼:
複製代碼 代碼如下:
public function downloads($name){
$name_tmp = explode("_",$name);
$type = $name_tmp[0];
$file_time = explode(".",$name_tmp[3]);
$file_time = $file_time[0];
$file_date = date("Y/md",$file_time);
$file_dir = SITE_PATH."/data/uploads/$type/$file_date/";
if (!file_exists($file_dir.$name)){
header("Content-type: text/html; charset=utf-8");
echo "File not found!";
exit;
} else {
$file = fopen($file_dir.$name,"r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir . $name));
Header("Content-Disposition: attachment; filename=".$name);
echo fread($file, filesize($file_dir.$name));
fclose($file);
}
}