【问题标题】:secure and resume support file downloading with php使用 php 安全和恢复支持文件下载
【发布时间】:2009-09-22 12:18:40
【问题描述】:

我想授予我的一些网站用户下载特定文件的权限。 为了方便用户,下载需要恢复支持。

出于这个原因,我想到用这段代码来保护文件:

// Check for user session and privileges if it's okay continue
ob_end_clean();
$data = file_get_contents("c:\\newdata.pdf");
$showname = "newdata.pdf";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"".$showname."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($data));
echo $data;

问题在于,它不仅不支持恢复,而且不会给我的网络服务器带来很大的开销,而且对于大文件以及大于 134217728 字节的文件,file_get_contents 需要大量内存,您将收到以下错误消息:

允许的内存大小为 134217728 字节 用尽(试图分配188862464 字节)

有什么建议吗?

【问题讨论】:

标签: php download


【解决方案1】:

【讨论】:

  • 它的字节服务没有下载:(
【解决方案2】:

使用readfile($fileName) 将文件数据直接传递给客户端。

编辑:如果您想要恢复支持,您必须编写一个允许您指定起始字节的 readfile 版本。这很容易通过fopen/fread 完成。无需将所有内容读入内存 - 您只需读取一个块,发送它,读取另一个,发送它......等等

【讨论】:

  • 此方法不支持恢复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 2012-06-18
  • 2013-11-14
  • 2013-04-22
  • 2011-11-03
  • 2013-05-03
  • 1970-01-01
相关资源
最近更新 更多