找回密码
 注册会员
查看: 565|回复: 0

PHP 文件下载

[复制链接]
发表于 2010-7-1 09:31:09 | 显示全部楼层 |阅读模式
<p>PHP 文件下载</p>
<p><img src="http://img.baidu.com/img/iknow/icn_point.gif"> 悬赏分:0 -</p>
<p>解决时间:2010-7-1 09:30</p>
<p><?php</p>
<p>$file_name=$name;</p>
<p>$file_dir=$user;</p>
<p>$file_dir = $file_dir."/";</p>
<p>if (!file_exists($file_dir.$file_name)){</p>
<p>echo "文件找不到";</p>
<p>exit;</p>
<p>} else {</p>
<p>$file = fopen($file_dir . $file_name,"r");</p>
<p>Header("Content-type: application/octet-stream");</p>
<p>Header("Accept-Ranges: bytes");</p>
<p>Header("Accept-Length: ".filesize($file_dir . $file_name));</p>
<p>Header("Content-Disposition: attachment; filename=" . $file_name);</p>
<p>echo fread($file,filesize($file_dir . $file_name));</p>
<p>fclose($file);</p>
<p>exit();</p>
<p>}</p>
<p>?></p>
<p>这是大体的下载代码,但是我发现好像很慢,用迅雷下载得不到文件大小!!!</p>
<p>就是没有直接下载快!</p>
<p>提问者: 421929478 - 四级</p>
<p>最佳答案</p>
<p>你的header少了点东西,看我这个代码:</p>
<p>还有文件读取的时候要用while,一次读取8192字节的内容,不然遇到大文件,有可能无法下载的。</p>
<p>$file = fopen($DOC_ID,"r"); // 打开文件</p>
<p>header(\'Cache-Control: pre-check=0, post-check=0, max-age=0\');</p>
<p>header("Content-type: application/octet-stream");</p>
<p>header("Accept-Ranges: bytes");</p>
<p>header("Accept-Length: ".filesize($DOC_ID));</p>
<p>header("Content-Length: " .filesize($DOC_ID));  //这个返回文件大小</p>
<p>header("Content-Disposition: attachment; filename=".$DOC_NAME);</p>
<p>$contents="";</p>
<p>while ( !feof( $file ) )</p>
<p>{</p>
<p>echo $contents = fread( $file, 8192 );</p>
<p>}</p>
<p>fclose($file);</p>
<p>exit();</p>
<p>0</p>
<p>回答者:</p>
<p><img src="http://www.baidu.com/search/zhidao/zhuti-5year/image/icon1.gif"></p>
<p>rogertl - 九级   2010-6-30 16:27</p>
<p>我来评论>></p>
<p>提问者对于答案的评价:</p>
<p>后来我才发现Accept-Length改为Content-Length就可以了!!!!</p>
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|文字版|手机版|小黑屋|襄阳城

GMT+8, 2025-5-16 23:44

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表