PHP提供的下载每次在X个KB之后中断

时间:2011-06-26 16:15:29

标签: php nginx cpanel apache2.2

我编写了一个代码,可以让访问者下载一​​个1.1M大小的csv文件。如果访问此脚本,下载将在30-40K左右中断(如下面wget输出中所示),而如果他通过http://domain.com/events.csv之类的直接链接下载,则可以正常工作。我相信这与服务器上的php配置值有关,但我几乎使用了所有值[相关和不相关],例如

  • post_max_size [upto 90M]
  • max_file_upload [upto 90M]
  • max_execution_time [0 and upto 600]
  • max_input_time [0 and upto 600]
  • memory_limit [upto 1024M]

以下包含我的代码:

 <?php

$realpath="/home/user/public_html/events.csv";
$size = intval(sprintf("%u", filesize($realpath)));

@ini_set('zlib.output_compression', 0);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=events.xls"); 
header("Content-Transfer-Encoding: binary ");
header("Pragma: public");
header('Cache-Control: no-store, no-cache, must-revalidate'); 
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ("Pragma: no-cache");
header("Expires: 0");
header("Content-Description: File Transfer");
header("Content-Type: text/csv");
header("Content-Length: ". $size);

// also tried with having a flush(); here
// also tried with file_get_contents();
//also tried with wrapping the file_get_contents() or readfile() call inside ob_start() and ob_flush()+ob_clean()
readfile($realpath);
exit;

?>

这是wget输出

wget "http://domain.com/test.php"
--2011-06-26 19:47:55--  http://domain.com/test.php
Resolving domain.com... 69.117.110.115
Connecting to domain.com|69.117.110.115|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1139844 (1.1M) [text/csv]
Saving to: `test.php'

 3% [===>                                                                                                                            ] 36,373      --.-K/s   in 11s     

2011-06-26 19:48:11 (3.29 KB/s) - Connection closed at byte 36373. Retrying.

--2011-06-26 19:48:12--  (try: 2)  http://domain.com/test.php
Connecting to domain.com|69.117.110.115|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1139844 (1.1M) [text/csv]
Saving to: `test.php'

 3% [===>                                                                                                                            ] 40,469      --.-K/s   in 11s     

2011-06-26 19:48:24 (3.66 KB/s) - Connection closed at byte 40469. Retrying.

如果我删除提供下载所需的标题()并回显内容,那么Chrome显示test.php大约是1.09M加上一些更多请求,加起来高达1.1M [即使在这种情况下测试wget] .php显示与上面相同的行为],而firefox firebug显示请求介于140K-300K之间[仍未显示所有内容。

1 个答案:

答案 0 :(得分:0)

检查http://www.php.net/manual/en/function.readfile.php并搜索:large。复制粘贴代码:)