下载问题,小文件下载正常,下载大于100M的文件,进度条到100M以外的时候,进度条就不走了,代码什么地方出问题了? 本帖最后由 bxline 于 2012-12-17 06:31:43 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简单看了一下,发现好几个问题第一,超时时间太短第二,一次性读入一个大文件到变量似乎很不合适给你一段我用过的代码,经测试,他可以下载超过10G的文件,代码如下,根据你的要求修改一下吧 Case 'cat': //讀取文件 => 讀取的內容 => C('cat',$file); $o[1] = RealPath($o[1]); IF(!$o[1]) {Return False;} IF(!Is_Readable($o[1])) {C('error',$o[1].' is can\'t to read!',1);} IF($f = C('atr',$o[1])) { Header("Content-Length:{$f['size']}"); Header('Content-Description:File Transfer'); Header('Content-Transfer-Encoding:binary'); Header('Expires:0'); Header('Cache-Control:must-revalidate'); Header('Pragma:public'); IF(Function_exists("mime_content_type")) { Header("Content-Type: ".mime_content_type($f['name'])); }Else{ Header("Content-Type: application/octet-stream"); Header("Content-Disposition: attachment; filename={$f['name']}"); } //Echo File_get_contents($o[1]); $handle = @fopen($o[1],'rb'); IF($handle) { while (!feof($handle)) { Echo fgets($handle,4096); } } fclose($handle); }Else{ C('error',$o[1].' read error!',1); } Unset($f); Break; echo 100 * 1024 / 300;得 341 K/s即需要平均带宽为 3M 以上你的下载代码不具有断点续传能力,一旦出现传输错误,即可终止 sibang:您好,您的代码写的非常好,谢谢您了,但是我应该如何调用呢,还望您指点指点,非常谢谢您第1处:$o[1]代表的意思是我代码中的$filename一样吗?$filename这个变量是文件的路径,我可以把所有$o[1]用$filename替代吗?第2处:$f = C('atr',$o[1])) 其中的atr 是什么含义,我不知道怎么去调用这个atr。假如现在知道了$filename变量就是文件路径,就下载这个$filename变量,如何使用您这段代码呢? 我该写了一下,你看看Function down($file){ IF(!$file Or !File_Exists($file) Or !Is_Readable($file)) {Return False;} IF($f = atr($file)) { Header("Content-Length:{$f['size']}"); Header('Content-Description:File Transfer'); Header('Content-Transfer-Encoding:binary'); Header('Expires:0'); Header('Cache-Control:must-revalidate'); Header('Pragma:public'); IF(Function_exists("mime_content_type")) { Header("Content-Type: ".mime_content_type($f['name'])); }Else{ Header("Content-Type: application/octet-stream"); Header("Content-Disposition: attachment; filename={$f['name']}"); } //Echo File_get_contents($file); $handle = @fopen($file,'rb'); IF($handle) { while (!feof($handle)) { Echo fgets($handle,4096); } } fclose($handle); }Else{ Dir('error',$file.' read error!'); } Unset($f);}Function atr($file){ $m = array(); $m['name'] = basename($file); $m['type'] = filetype($file); $m['real_path'] = RealPath($file); $m['time'] = filemtime($file); $m['size'] = filesize($file); $m['writeable'] = Is_Writeable($file)?True:False; $m['readable'] = Is_Readable($file)?True:False; $m['chmod'] = substr(sprintf('%o', fileperms($file)),-4); Return $m;} 非常感谢,我赶紧试验了一下,用您写给我的上面代码,下载个445MB的文件,但是下到145M就提成下载完成了,不象以前提示出错了,这次提示完成了,但是还有300多MB没有下载,怎么会提示完成了呢?? 最后一个问题就结贴啦:sibang:请问最后您写的代码安全性如何?fgets用下载安全么,我是新手,问的唐突还忘海涵一下 动态循环输出表格的换行问题~~~~~~~ 初学PHP遇到错误了,高手帮忙解答 求一个功能的实现 mysql求平均值的问题,急! php怎样接收远程post xml文档 关于php工资问题??? 怎样取得上传文件的文件名? 在 WIN 2K 下如何配置PHP服务 如何实现刷新(关于聊天室) 我想在load页面时动态地生成列表。 mysql根据有没数据返回不同结果集 apache服务器首次访问慢!
第一,超时时间太短
第二,一次性读入一个大文件到变量似乎很不合适
给你一段我用过的代码,经测试,他可以下载超过10G的文件,代码如下,根据你的要求修改一下吧
Case 'cat': //讀取文件 => 讀取的內容 => C('cat',$file);
$o[1] = RealPath($o[1]);
IF(!$o[1]) {Return False;}
IF(!Is_Readable($o[1])) {C('error',$o[1].' is can\'t to read!',1);}
IF($f = C('atr',$o[1])) {
Header("Content-Length:{$f['size']}");
Header('Content-Description:File Transfer');
Header('Content-Transfer-Encoding:binary');
Header('Expires:0');
Header('Cache-Control:must-revalidate');
Header('Pragma:public'); IF(Function_exists("mime_content_type")) {
Header("Content-Type: ".mime_content_type($f['name']));
}Else{
Header("Content-Type: application/octet-stream");
Header("Content-Disposition: attachment; filename={$f['name']}");
}
//Echo File_get_contents($o[1]); $handle = @fopen($o[1],'rb');
IF($handle) {
while (!feof($handle)) {
Echo fgets($handle,4096);
}
}
fclose($handle); }Else{
C('error',$o[1].' read error!',1);
}
Unset($f);
Break;
得 341 K/s
即需要平均带宽为 3M 以上你的下载代码不具有断点续传能力,一旦出现传输错误,即可终止
第2处:$f = C('atr',$o[1])) 其中的atr 是什么含义,我不知道怎么去调用这个atr。假如现在知道了$filename变量就是文件路径,就下载这个$filename变量,如何使用您这段代码呢?
Function down($file){
IF(!$file Or !File_Exists($file) Or !Is_Readable($file)) {Return False;} IF($f = atr($file)) {
Header("Content-Length:{$f['size']}");
Header('Content-Description:File Transfer');
Header('Content-Transfer-Encoding:binary');
Header('Expires:0');
Header('Cache-Control:must-revalidate');
Header('Pragma:public');
IF(Function_exists("mime_content_type")) {
Header("Content-Type: ".mime_content_type($f['name']));
}Else{
Header("Content-Type: application/octet-stream");
Header("Content-Disposition: attachment; filename={$f['name']}");
}
//Echo File_get_contents($file);
$handle = @fopen($file,'rb');
IF($handle) {
while (!feof($handle)) {
Echo fgets($handle,4096);
}
}
fclose($handle);
}Else{
Dir('error',$file.' read error!');
}
Unset($f);
}
Function atr($file){
$m = array();
$m['name'] = basename($file);
$m['type'] = filetype($file);
$m['real_path'] = RealPath($file);
$m['time'] = filemtime($file);
$m['size'] = filesize($file);
$m['writeable'] = Is_Writeable($file)?True:False;
$m['readable'] = Is_Readable($file)?True:False;
$m['chmod'] = substr(sprintf('%o', fileperms($file)),-4);
Return $m;
}