我写了一段代码,让访问者下载一个大小为1.1M的csv文件。如果一个人访问这个脚本,下载会在30-40K左右中断(如下面的wget输出所示),而如果他通过类似的直接链接下载http://domain.com/events.csv它工作得很好。我相信这与服务器上的php配置值有关,但我几乎玩过所有的值[相关和非相关],如
- post_max_size[高达90M]
- 最大文件上传量[高达90M]
- 最大执行时间[0至600]
- 最大输入时间[0至600]
- 内存限制[高达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[即使在这种情况下,test.php的wget也显示出与上面相同的行为],而firefoxfirebug显示请求在140K-300K之间[仍然没有显示所有内容。
检查http://www.php.net/manual/en/function.readfile.php和搜索:大。复制粘贴代码:)