【问题标题】:PHP readfile vs. file_get_contentsPHP 读取文件与 file_get_contents
【发布时间】:2013-11-20 19:59:29
【问题描述】:

我使用以下代码生成 zip

// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);

这段代码运行良好,但由于未知原因在我尝试之前无法运行

// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
echo file_get_contents($zip_name);

我很想知道这两种情况发生了什么

【问题讨论】:

  • 你的意思是file_get_contents 对吧?
  • “不工作”是什么意思?您是否尝试将错误报告设置为适当的值并阅读任何错误/警告消息?
  • 是的,我这样做了,它显示的是一个空白页,只有一个 \n
  • @Shankar 是的 file_get_contents 第一次工作等等

标签: php function filesystems readfile


【解决方案1】:

Readfile 将文件直接读入输出缓冲区,file_get_contents 将文件加载到内存中,当您回显结果时,数据从内存复制到输出缓冲区有效地使用了 readfile 内存的 2 倍。

【讨论】:

  • 是的,这就是我的观察结果,readfile 在生成时试图抛出文件,但 file_get_contents 会等到它成功生成。
猜你喜欢
  • 2016-03-08
  • 2017-01-04
  • 2016-04-20
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 2012-06-19
相关资源
最近更新 更多