2009-09-22 78 views
1

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

出于这个原因,它来到我的脑海的文件保护与这段代码:

// 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; 

的问题是,不仅它不会恢复的支持也它gonno给我的web服务器一笔大开销,也需要的file_get_contents大文件也超过134217728个字节的文件大很多的内存,您将收到此错误信息:

的134217728个字节

允许的内存容量用尽(试图分配188862464个 字节)

有什么建议吗?

+0

只有'应用程序/八位字节stream'是官方应该由客户端下载的二进制数据的媒体类型。 – Gumbo 2009-09-22 13:04:03

+0

可能的[在PHP中是否有好的部分文件下载实现?]的副本(http://stackoverflow.com/questions/1395656/is-there-a-good-implementation-of-partial-file-downloading-in -php) – 2013-04-04 11:31:16

回答

1

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

编辑:如果你想恢复支持,你必须编写一个readfile版本,允许你指定一个起始字节。这很容易通过fopen/fread完成。有没有必要去详细阅读到内存 - 你刚才读一大块,发送,读取另一个,发送...等

+0

这种方法不是恢复支持 – EBAG 2009-09-22 12:25:23

+0

对于你编辑:这不是那么简单 \ – EBAG 2009-09-22 15:25:51