php实现文件下载简单示例(代码实现文件下载)
本文导语: 代码如下: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)){...
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);
}
}
示例2:代码实现文件下载
一般实现下载都是调用url来下载,但是遇到ie能识别打开的文件就不能用这种方式了,比如下载一个图片、html网页等,这时就需要编程来实现,以下php代码可以解决: