|
下載文件的方法:
實列一:
<?php
header("Content-Type: text/html;charset=utf-8");
$file_name ="jdao.png";
//使用相對路徑
// $file_path = "./pic/".$file_name;
// 使用絕對路徑,下載速度要更快些
$file_path = $_SERVER['DOCUMENT_ROOT']."/pic/".$file_name;
if(!file_exists($file_path)){
echo "文件不存在";
return;
}
$fp = fopen($file_path,"r");
$file_size = filesize($file_path);
//echo $file_size;
header("Content-Type: application/octet-stream"); //返回為文件形式
header("Accept-Ranges: bytes");
header("Accept-Length: $file_size");
header("Content-Disposition: attachment; filecolor:#c82829">$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);
// echo $file_path; 這句話不能放在下載前,否則就執行不了下載
?>
實例2:
為了兼容中文文件名不出現亂碼,并做成函數源碼如下:
<?php
//首行前不能空出
//文件下載參數說明
//$file_name: 下載文件名
//$file_sub_path: 子文件夾路徑
function fileDown($file_name,$file_sub_path){
//$file_name = iconv("utf-8","gb2312",$file_name); //IGNORE
$file_path = $_SERVER['DOCUMENT_ROOT'].$file_sub_path .$file_name;
if(!file_exists($file_path)){
echo "文件不存在";
return;
}
$fp = fopen($file_path,"r");
$file_size = filesize($file_path);
//下載文件的頭部分
header("Content-Type: application/octet-stream"); //返回為文件形式
header("Accept-Ranges: bytes");
header("Accept-Length: $file_size");
//header("Content-Disposition: attachment; filewidth:14px;color:#8e908c">彈出下載對話框
//以下為了兼容中文文件名在不同瀏覽器中顯示而做如下修改
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($file_name);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filecolor:#c82829">$encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '"');
} else {
header('Content-Disposition: attachment; filecolor:#c82829">$file_name . '"');
}
$buffer = 11024;
$file_count = 0;
while((!feof($fp)) && ($file_size - $file_count > 0)){
$file_data = fread($fp,$buffer);
$file_count += $buffer;
//把數據回送給瀏覽器
echo $file_data;
}
fclose($fp);
}
fileDown("電腦圖片.jpg","/pic/");
?>
測試文件下載
測試中文文件下載
將文件下載封裝在類文件里filedown.class.php,源碼如下
<?php
class filedown{
protected $file_name;
public $file_sub_path;
function __construct($file_name,$file_sub_path){
$this->file_name = $file_name;
$this->file_sub_path = $file_sub_path;
}
function fileDown(){
//$file_name = iconv("utf-8","gb2312",$file_name); //IGNORE
$file_path = $_SERVER['DOCUMENT_ROOT'].$this->file_sub_path .$this->file_name;
if(!file_exists($file_path)){
echo "文件不存在";
return;
}
$fp = fopen($file_path,"r");
$file_size = filesize($file_path);
//下載文件的頭部分
header("Content-Type: application/octet-stream"); //返回為文件形式
header("Accept-Ranges: bytes");
header("Accept-Length: $file_size");
//header("Content-Disposition: attachment; filewidth:14px;color:#8e908c">彈出下載對話框
//以下為了兼容中文文件名在不同瀏覽器中顯示而修改的
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($this->file_name);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filecolor:#c82829">$encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $this->file_name . '"');
} else {
header('Content-Disposition: attachment; filecolor:#c82829">$this->file_name . '"');
}
$buffer = 11024;
$file_count = 0;
while((!feof($fp)) && ($file_size - $file_count > 0)){
$file_data = fread($fp,$buffer);
$file_count += $buffer;
//把數據回送給瀏覽器
echo $file_data;
}
fclose($fp);
}
}
// $file1 = new filedown("電腦圖片.jpg","/pic/");
// $file1->filedown();
?>
演示多文件下載
|
|