PHP文件下载函数的完整代码

PHP文件下载函数(代码)感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

/**
* PHP文件下载函数(代码)
*
* @param 
* @arrange 512-笔记网: jb51.cc
**/
function download($file_url,$new_name=''){
if(!isset($file_url)||trim($file_url)==''){
return '500';
}
if(!file_exists($file_url)){ //检查文件是否存在
return '404';
}
$file_name=basename($file_url);
$file_type=explode('.',$file_url);
$file_type=$file_type[count($file_type)-1];
$file_name=trim($new_name=='')?$file_name:urlencode($new_name).'.'.$file_type;
$file_type=fopen($file_url,'r'); //打开文件
//输入文件标签
header(Content-type: application/octet-stream);
header(Accept-Ranges: bytes);
header(Accept-Length: .filesize($file_url));
header(Content-disposition: attachment; filename=.$file_name);
//输出文件内容
echo fread($file_type,filesize($file_url));
fclose($file_type);
}
/***   来自编程之家 jb51.cc(jb51.cc)   ***/
注意:
以上检查文件是否存在是通过file_exists这个函数的,但这个函数只能检查相对于当前服务器网站目录里面的文件

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...