我编写了一个代码,让访问者可以下载一个 1.1M 大小的 csv 文件。如果访问此脚本,下载会中断大约 30-40K(如下 wget 输出所示),而如果他通过http://domain.com/events.csv之类的直接链接下载它,它就可以正常工作。我相信这与服务器上的 php 配置值有关,但我几乎玩过所有值[相关和不相关],例如
- post_max_size [最大 90M]
- max_file_upload [最大 90M]
- max_execution_time [0 到 600]
- max_input_time [0 到 600]
- memory_limit [高达 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.
如果我删除提供下载所需的 header() 并仅回显内容,则 Chrome 显示 test.php 大约为 1.09M 加上更多请求,加起来高达 1.1M [即使在这种情况下 wget 的 test.php 显示与上述相同的行为],而 firefox firebug 显示请求在 140K-300K 之间 [仍然没有显示所有内容。