【问题标题】:Disable folder public access from htaccess从 htaccess 禁用文件夹公共访问
【发布时间】:2014-09-12 14:31:51
【问题描述】:

我有一个网站,您可以从中下载资源。

我想要的是禁用存储项目的文件夹的公共访问权限,但允许网站访问它们以进行下载。

我在 .htaccess 中使用了Order deny,allow,但如果我使用它,我将无法从网站下载。

它会给我错误 404,与尝试为文件键入直接 url 相同。已执行下载。

我有以下代码:

       $link = $item->link;

        $link_headers = get_headers($link);

        //Headers
        if(isset($link_headers[0]))
        {
            header($link_headers[0]);

        }//if exists
        if(isset($link_headers[1]))
        {
            header($link_headers[1]);

        }//if exists
        if(isset($link_headers[2]))
        {
            header($link_headers[2]);

        }//if exists
        if(isset($link_headers[3]))
        {
            header($link_headers[3]);

        }//if exists
        if(isset($link_headers[4]))
        {
            header($link_headers[4]);

        }//if exists
        if(isset($link_headers[5]))
        {
            header($link_headers[5]);

        }//if exists
        if(isset($link_headers[6]))
        {
            header($link_headers[6]);

        }//if exists
        if(isset($link_headers[7]))
        {
            header($link_headers[7]);

        }//if exists
        if(isset($link_headers[8]))
        {
            header($link_headers[8]);

        }//if exists

        readfile($link);

        exit;

问题是,如果我将 .htaccess 和拒绝全部放入包含所有要下载的项目的文件夹中,那么下载将不再起作用,因为我没有权限,并且我希望仅对尝试下载的人可用从直接链接到来自 url 的文件。

【问题讨论】:

  • 您想用密码保护文件夹吗?您在寻找某种用户级别的访问限制吗?
  • 我想要的是不允许从直接链接访问文件。例如像这样访问 www.website.com/downloads/file32.jpg 应该返回权限被拒绝。
  • 您必须使用文件的路径。我猜你使用文件的链接。

标签: php .htaccess


【解决方案1】:

实现此目的的一种方法是通过检查 http-referrer 的脚本进行下载。标题当然可以手动修改,但我认为做一些比这更复杂的事情会有点矫枉过正:

/*
Change your download links to point to a php script, or use .htaccess to rewrite downloads to the same file
*/
<a href="download.php?file=somefile.zip">Download</a>

//download.php

if ($_SERVER['HTTP_REFERER'] == "your_downloads_page.html") {
    $file = $_GET['file'];
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
}

但是一定要添加一些安全检查,这样用户就不能下载他们不应该被允许的文件。也许过滤所有的斜线和双点。

【讨论】:

    【解决方案2】:

    如果您保护您的文件和文件路径,您可以使用 download.php 文件。

    下载.php文件内容

    <?php
    // Connect to db and get file information from db.
    // $db_filename and $db_filetype getting from db
    
    header("Content-disposition: attachment; filename=def.file_type"); // def.pdf
    header("Content-type: " . $db_filetype);  // application/pdf
    readfile("/path/of/your/files/".$db_filename); // /project/home/downloads/abc.pdf
    

    用法:

    download.php?file_id=1
    

    这种用法是保护您的文件夹和文件名,但它使用数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2011-01-30
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      相关资源
      最近更新 更多