代码之家  ›  专栏  ›  技术社区  ›  Decent Dabbler

下载问题:速度慢和/或失败

  •  0
  • Decent Dabbler  · 技术社区  · 14 年前

    我使用以下脚本让访问者下载文件:

    header( 'Content-Type: application/octet-stream' );
    header( 'Content-Transfer-Encoding: binary' );
    header( 'Content-Disposition: attachment; filename=' . $fileName );
    header( 'Content-Length: ' . filesize( $filePath ) );
    header( 'Content-Description: Download' );
    header( 'Cache-Control: private' );
    header( 'Pragma: no-cache' );
    header( 'Expires: 0' );
    
    readfile( $filePath );
    exit();
    

    它不太好用(我还将文件名加了引号,结果相同)。

    它的运行速度非常慢,有时下载甚至会停止。尤其是在Opera中,99%的下载都会暂停。有时它甚至会立即显示99%已完成,然后开始下载并在34%左右停止。

    通过使用Firefox的Live HTTP headers插件,我注意到服务器在响应中添加了传统的头:

    HTTP/1.1 200 OK
    Date: Thu, 18 Feb 2010 09:27:25 GMT
    Server: Apache
    X-Powered-By: PHP/5.2.12
    Content-Transfer-Encoding: binary
    Content-Disposition: attachment; filename=test.psd
    Content-Length: 398635
    Content-Description: Download
    Cache-Control: private
    Pragma: no-cache
    Expires: 0
    Content-Encoding: gzip // <-- expecially this one,
    Vary: Accept-Encoding // <-- this one,
    MS-Author-Via: DAV // <-- and this one
    Keep-Alive: timeout=10, max=100
    Connection: Keep-Alive
    Content-Type: application/octet-stream
    

    这些可能是问题的原因吗?

    当我在本地主机上运行脚本时,一切正常。另外,当我直接从这个主机下载文件时,速度也很好,很流畅。

    在这一点上我真的很无知。谢谢你的帮助。先谢谢你。

    更新:

    我想我已经把问题缩小到了瓶颈。Web服务器自动压缩输出。当我取下 Content-Length 内容长度 与实际的gzip输出不再匹配。在PHP中,我读取未压缩的文件大小来设置 内容长度

    内容长度 Web服务器自动gzip压缩输出时的标头大小。

    3 回复  |  直到 14 年前
        1
  •  0
  •   initall    14 年前

    尝试取消设置gzip内容编码。

    使用 ob_start() 在你剧本的开头;在设置标题之前,请使用 @ob_end_clean(); 紧接着,明确地设定 header("Content-Encoding:"); 尝试取消设置可能传入的任何gzip编码。 在你档案的末尾 @ob_end_flush(); .

        2
  •  0
  •   Marco Demaio    14 年前

    我使用下面的代码,它是有效的。说实话,我还没有完全理解我发送的所有标题,我仍然没有时间调查,我在以下内容中找到了解释:

    资料来源:

    http://www.opendesigns.org/forum/discussion/1437/php-download-counter/#pgbottom http://www.webdeveloper.com/forum/showthread.php?t=115815&highlight=PHP+download+counter http://php.net/manual/en/function.header.php#83384

       /*
       TODO: still to be read and better understood.
       */
    
       //no caching (I don't uderstand what is this part useful for)
       header("Pragma: public"); //?
       header("Expires: 0"); //?
       header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //?
       header("Cache-Control: private", false); //?
    
       //sending download file
       header("Content-Type: application/octet-stream"); //application/zip can use application/octet-stream that is more generic it works because in now days browsers are able to detect file anyway
       header("Content-Disposition: attachment; filename=\"" . basename($file_serverfullpath) . "\""); //ok
       header("Content-Transfer-Encoding: binary"); //?
       header("Content-Length: " . filesize($file_serverfullpath)); //ok
       readfile($file_serverfullpath);
    
        3
  •  0
  •   Decent Dabbler    14 年前

    我已经把问题缩小到了瓶颈。Web服务器自动压缩输出。当我从PHP脚本中删除内容长度标题时,一切都开始顺利下载。这很有意义:内容长度的值与实际的gzip输出不再匹配。在PHP中,我读取未压缩的文件大小来设置内容长度头,但之后,Apache会对其进行压缩,这可能是浏览器阻塞的地方。