PHP readfile 返回零长度文件

标签 php readfile

这很很奇怪

我有一个脚本可以通过浏览器将本地 zip 文件发送给用户。到目前为止,该脚本运行良好,没有任何问题。今天我的同事通知我脚本正在发送零长度文件。

一些背景信息:

  • 在脚本出错之前没有修改服务器设置
  • 测试了不同的浏览器(在 Chrome/Firefox 上相同)
  • 以前的 zip 文件(以前运行良好)也是零长度
  • 脚本在服务器上找到文件
  • 文件大小(回显调试时)是正确的
  • 试图将服务器设置和脚本调整为 adviced here没有成功。

更新:

  • is_readable() 返回 1
  • 文件大小可能在 5Mb 到 100Mb 之间(不具体)
  • $zip_file 保存文件路径
  • $zip_name 保存 zip 名称
  • 文件实际上是零长度的(在文本编辑器中打开它不包含一个字节)
  • error_reportingOn (E_ALL) 什么都不显示
  • 没有标题,浏览器会正确显示 zip“源”
  • Safari 说:'0 字节?无法解码原始数据'第一个有用的(?)症状

有问题的片段:

if (file_exists($zip_file)) {
    header('Content-type: application/zip');
    header('Content-disposition: filename="' . $zip_name . '"');
    header("Content-length: " . filesize($zip_file));
    readfile($zip_file);
    exit();
}

我怎样才能轻松调试它?

提前致谢,法布里克

最佳答案

http://www.php.net/manual/en/function.readfile.php#102137 :

It should be noted that in the example:

header('Content-Length: ' . filesize($file));

$file should really be the full path to the file. Otherwise content length will not always be set, often resulting in the dreaded "0 byte file" problem.

关于PHP readfile 返回零长度文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375143/

相关文章:

node.js - Node.js 如何使用单进程实现异步回调

php - 如何用php检查字符串是否在数组中?

php - 无法将mysqli_result类的对象转换为C:\wamp\www\webservice_mine.php中的字符串

python - 在python中读取yaml文件的行 block 并对其进行排序

java - 使用scanner.useDelimiter读取输入文件的第一个字符不正确

c++ - 如何将该程序从 C++ 转换为 C?

ubuntu 14.04 LTS 5.5 和 7.1 之间的 PHP 版本冲突

Php - 您的 PHP 安装似乎缺少 WordPress 所需的 MySQL 扩展

PHP:在特定日期自动更新数据库字段,没有通过 cronjob 的脚本

c++ - 在 C++ 中从文件读取到节点