本文章為你提供這款php教程檔下載代碼是一款利用header把檔代碼發送到用戶端的瀏覽器進行下載哦。
<?
function download($file_dir,$file_name)
//參數說明:
//file_dir:檔所在目錄
//file_name:檔案名
{
$file_dir = chop($file_dir);//去掉路徑中多餘的空格
//得出要下載的檔的路徑
if( $file_dir != '')
{
$file_path = $file_dir;
& nbsp; if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
$file_path .= '/';
$file_path .= $file_name;
}
else
$file_path = $file_name;
//判斷要下載的檔是否存在www.jzread.com
if(!file_exists($file_path ))
{
echo '對不起,你要下載的檔不存在。 ';
return false;
}
$file_size = filesize($file_path);
header("Content-type: application/octet-stream");
header(" Accept-Ranges: bytes");//jzread.com
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;
}
?>