上一篇:PHP判断远程链接URL是否存在
讨论数量:3
楼主补充:
//下载文件函数
function download($file_path,$down_name){
//参数说明:
//file_path:文件所在目录(包括文件名)
//down_name:下载时显示的文件名
if(empty($down_name)){ $down_name=$file_path; }
//判断要下载的文件是否存在
if(!file_exists($file_path))
{
echo '对不起,你要下载的文件不存在。'.$file_path;
return false;
}
$file_size = filesize($file_path);
header("Content-type: application/octet- tream");
header("Accept-Range : byte ");
header("Accept-Length: $file_size");
header("Content-Disposition: attachment; filename=".$down_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;
}
//==================================
//调用方法:
$FileName='./upfiles/data.txt';
download($FileName,'data.txt');