本帖最后由 bxline 于 2012-12-17 06:31:43 编辑

解决方案 »

  1.   

    简单看了一下,发现好几个问题
    第一,超时时间太短
    第二,一次性读入一个大文件到变量似乎很不合适
    给你一段我用过的代码,经测试,他可以下载超过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;
      

  2.   

    echo 100 * 1024 / 300;
    得 341 K/s
    即需要平均带宽为 3M 以上你的下载代码不具有断点续传能力,一旦出现传输错误,即可终止
      

  3.   

    sibang:您好,您的代码写的非常好,谢谢您了,但是我应该如何调用呢,还望您指点指点,非常谢谢您第1处:$o[1]代表的意思是我代码中的$filename一样吗?$filename这个变量是文件的路径,我可以把所有$o[1]用$filename替代吗?
    第2处:$f = C('atr',$o[1]))  其中的atr 是什么含义,我不知道怎么去调用这个atr。假如现在知道了$filename变量就是文件路径,就下载这个$filename变量,如何使用您这段代码呢?
      

  4.   

    我该写了一下,你看看
    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;
    }
      

  5.   

    非常感谢,我赶紧试验了一下,用您写给我的上面代码,下载个445MB的文件,但是下到145M就提成下载完成了,不象以前提示出错了,这次提示完成了,但是还有300多MB没有下载,怎么会提示完成了呢??
      

  6.   

    最后一个问题就结贴啦:sibang:请问最后您写的代码安全性如何?fgets用下载安全么,我是新手,问的唐突还忘海涵一下