在 PHP/Apache 中提供大型受保护文件
我需要从 Apache Web 服务器提供大文件(> 2GB)。这些文件是受保护的下载,因此我需要某种方式来授权用户。我正在使用的 CMS 使用根据 MySQL 数据库检查的 cookie 来验证用户。在服务器上,我无法控制 max_execution_time,并且对 memory_limit 的控制有限。
我的技术一直适用于小文件。用户在 PHP 中获得授权(通过 CMS)后,我使用 readfile() 来提供文件,该文件存储在文档根目录之上以防止直接访问。我读过有关对下载进行分块或使用 fpassthru 来绕过 PHP 内存限制的技术。但我还没有找到解决 max_execution_time 限制的技术。
我考虑过将文件存储在文档根目录中,这样我们就可以完全绕过 PHP。但我不知道如何使用 htaccess 限制访问。我需要先根据数据库验证用户,然后才能向他们提供文件。
谢谢。
I need to serve up large files (> 2gb) from an Apache web server. The files are protected downloads, so I need some kind of way to authorize the user. The CMS I'm using uses cookies checked against a MySQL database to verify the user. On the server, I have no control over max_execution_time, and limited control over memory_limit.
My technique has been working for small files. After the user has been authorized in PHP (by the CMS), I use readfile() to serve the file, which is stored above the document root to prevent direct access. I've read about techniques to chunk the download or to use fpassthru to get around the PHP memory limit. But I haven't found a technique to get around the max_execution_time limit.
I thought about storing the file within the document root, so we could bypass PHP entirely. But what I can't figure out is how to restrict access with htaccess. I need to verify the user against the database before I can serve them the file.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的解决方案:在 Apache 中安装
mod_xsendfile
, PHP 脚本对用户进行授权,并在成功后发送带有指向受保护文件位置的X-Sendfile
标头的响应。从那时起,Apache 将负责将文件提供给客户端;不是 PHP。The nicest solution in my opinion: install
mod_xsendfile
in your Apache, have the PHP script authorize the user, and on success send a response with anX-Sendfile
header pointing to the location of the protected file. From that point on, Apache does the work of serving the file to the client; not PHP.看看
set_time_limit()
http://www.php.net/manual/en/ function.set-time-limit.php
和
max_execution_time
http://www.php.net/手册/en/info.configuration.php#ini.max-execution-time
Take a look at
set_time_limit()
http://www.php.net/manual/en/function.set-time-limit.php
and
max_execution_time
http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time
使用符号链接怎么样?如果您有一个文件夹示例:
基本示例:
用法:
当用户付款时:
当用户想要下载他们的电子书时
这可能不是最便携的方法,但因为您正在重定向网络服务器正在处理下载的用户。
What about using symlinks? If you have a folder example:
Basic Example:
usage:
When the user pays:
When the user wants to download their ebook
This may not be the most portable method, but because you're redirecting the user the web server is handling the downloads.