How can I allow a user to download a file which is stored outside of the webroot?(如何允许用户下载存储在 webroot 之外的文件?)
问题描述
我正在开发一个允许注册用户(可以是任何人)上传文件的系统.我阻止了 mime 类型等,试图将文件限制为 .doc、.docx 和 .pdf 类型,但为了提高安全性,它们被上传到 webroot 之外的文件夹.
I am developing a system which allows registered users (who could be anybody) to upload files. I've block mime-types etc. to attempt to restrict the files to .doc, .docx and .pdf types, but for additional security, they are uploaded to a folder outside the webroot.
其他用户可以选择下载文件.我如何允许他们这样做?显然,我不能只添加指向该文件的链接,因为它在 webroot 之外.我不确定如何访问该文件!我想我可以使用 php 文件函数来访问该文件,但是我如何将它提供"给请求它的用户?
Other users can then choose to download the files. How do I allow them to do that? Obviously I can't just put in a link to the file, as it's outside the webroot. I'm not sure how to reach the file though! I presume I can use the php file functions to get to the file, but how do I then 'serve it up' to the user who has requested it?
这一切可能会带来哪些安全隐患?
What security implications might all of this have?
谢谢.
推荐答案
您需要一个执行以下操作的 PHP 脚本:
You need a PHP script that does the following:
- 正确设置内容类型标头(取决于用户正在下载的内容)
- 正确设置内容长度标头(取决于文件大小)
- 打开文件进行读取(可以使用fopen)
- 读取文件并将其内容输出到输出流
- 完成
您也可以使用 readfile 函数来做基本相同的事情.这是 PHP 网站上的一个示例:
You can also use readfile function to do basically the same. Here's an example from PHP's site:
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
这篇关于如何允许用户下载存储在 webroot 之外的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何允许用户下载存储在 webroot 之外的文件?
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
