[php]代码库
/**
* 下载文件
* Enter description here ...
* @param unknown_type $file_path 文件所在路径
* @param unknown_type $file_name 文件名
*/
function download($file_path,$file_name){
//判断要下载的文件是否存在
if(!file_exists($file_path)){
echo '对不起,你要下载的文件不存在。';
return false;
}
$file_size = filesize($file_path);
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: $file_size");
header("Content-Disposition: attachment; filename=".$file_name);
$fp = fopen($file_path,"r");
$buffer_size = 1024;
$cur_pos = 0;
while(!feof($fp)&&$file_size-$cur_pos>$buffer_size){
$buffer = fread($fp,$buffer_size);
echo $buffer;
$cur_pos += $buffer_size;
}
$buffer = fread($fp,$file_size-$cur_pos);
echo $buffer;
fclose($fp);
return true;
}
[代码运行效果截图]