【发布时间】: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 字节)
有什么建议吗?
【问题讨论】:
-
只有
application/octet-stream是应该由客户端下载的二进制数据的官方媒体类型。