1. <small id='6SsDd'></small><noframes id='6SsDd'>

      <legend id='6SsDd'><style id='6SsDd'><dir id='6SsDd'><q id='6SsDd'></q></dir></style></legend>
    1. <tfoot id='6SsDd'></tfoot>
      • <bdo id='6SsDd'></bdo><ul id='6SsDd'></ul>
    2. <i id='6SsDd'><tr id='6SsDd'><dt id='6SsDd'><q id='6SsDd'><span id='6SsDd'><b id='6SsDd'><form id='6SsDd'><ins id='6SsDd'></ins><ul id='6SsDd'></ul><sub id='6SsDd'></sub></form><legend id='6SsDd'></legend><bdo id='6SsDd'><pre id='6SsDd'><center id='6SsDd'></center></pre></bdo></b><th id='6SsDd'></th></span></q></dt></tr></i><div id='6SsDd'><tfoot id='6SsDd'></tfoot><dl id='6SsDd'><fieldset id='6SsDd'></fieldset></dl></div>

      如何允许用户下载存储在 webroot 之外的文件?

      时间:2023-05-21

      <i id='se3jJ'><tr id='se3jJ'><dt id='se3jJ'><q id='se3jJ'><span id='se3jJ'><b id='se3jJ'><form id='se3jJ'><ins id='se3jJ'></ins><ul id='se3jJ'></ul><sub id='se3jJ'></sub></form><legend id='se3jJ'></legend><bdo id='se3jJ'><pre id='se3jJ'><center id='se3jJ'></center></pre></bdo></b><th id='se3jJ'></th></span></q></dt></tr></i><div id='se3jJ'><tfoot id='se3jJ'></tfoot><dl id='se3jJ'><fieldset id='se3jJ'></fieldset></dl></div>
          <bdo id='se3jJ'></bdo><ul id='se3jJ'></ul>
          <legend id='se3jJ'><style id='se3jJ'><dir id='se3jJ'><q id='se3jJ'></q></dir></style></legend>

          <small id='se3jJ'></small><noframes id='se3jJ'>

        • <tfoot id='se3jJ'></tfoot>

                <tbody id='se3jJ'></tbody>
              • 本文介绍了如何允许用户下载存储在 webroot 之外的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在开发一个允许注册用户(可以是任何人)上传文件的系统.我阻止了 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:

                1. 正确设置内容类型标头(取决于用户正在下载的内容)
                2. 正确设置内容长度标头(取决于文件大小)
                3. 打开文件进行读取(可以使用fopen)
                4. 读取文件并将其内容输出到输出流
                5. 完成

                您也可以使用 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 之外的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PHP 上传文件 - 仅图像检查 下一篇:HTML5 输入类型文件的多个属性在 IE 中不起作用

                相关文章

                最新文章

              • <small id='FF1gT'></small><noframes id='FF1gT'>

              • <i id='FF1gT'><tr id='FF1gT'><dt id='FF1gT'><q id='FF1gT'><span id='FF1gT'><b id='FF1gT'><form id='FF1gT'><ins id='FF1gT'></ins><ul id='FF1gT'></ul><sub id='FF1gT'></sub></form><legend id='FF1gT'></legend><bdo id='FF1gT'><pre id='FF1gT'><center id='FF1gT'></center></pre></bdo></b><th id='FF1gT'></th></span></q></dt></tr></i><div id='FF1gT'><tfoot id='FF1gT'></tfoot><dl id='FF1gT'><fieldset id='FF1gT'></fieldset></dl></div>

                • <bdo id='FF1gT'></bdo><ul id='FF1gT'></ul>
                  <legend id='FF1gT'><style id='FF1gT'><dir id='FF1gT'><q id='FF1gT'></q></dir></style></legend>
                  <tfoot id='FF1gT'></tfoot>