0

我使用基于 PHP 的登录身份验证机制来允许/限制对我网站的某些部分(文件夹module1module2等)的访问,但我在限制对文件的访问时遇到了问题。我使用了文档文件夹(如下所示)来托管一些可下载的文件。这些文件的链接出现在 index.php 中(托管在目录中)。但是,如果由于某种原因,未经授权的用户获得了文件中文件的 URL,他将能够下载它。

/
/documents/
/module1/
/module2/

PS:因为这是一个内网网站,所以我限制了通过 IP 访问文档,但是仍然有很小的机会有人使用具有允许 IP 地址的 PC 并且他有文档的 URL。

4

1 回答 1

0

使用某种代理 PHP 脚本,该脚本将为用户提供文件,而无需提供真实的源位置。

然后用户会看到http://yourdomain.com/download.php?file=mydoc.docx

真正的路径仍然是 /documents/userid/2342/mydoc.docx 或者你的结构是什么样的。

然后让您的 download.php 文件通过以下方式提供文件:

<?php
// Validate the user here

// Set document root
$root = 'documents/userid/'.$userID.'/';

// Requested file
$file = $_GET['file'];

// Validate
if (file_exists($root . $file))
{
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($root . $file));

    ob_clean();
    flush();
    readfile($root . $file);
}
else { echo "File not found"; }
?>

在这里查看更多

于 2012-04-11T17:40:52.923 回答