【问题标题】:Routing *.zip file requests one level above, with open_basedir in effect路由 *.zip 文件请求上一层,open_basedir 生效
【发布时间】:2018-04-23 16:38:26
【问题描述】:

如果 open_basedir 生效,是否可以使用 .htaccess 将 zip 文件请求路由到 public_html 级别?

我问的原因是,如果访问者点击看似直接的下载链接,我会尝试强制下载与 public_html / www 处于同一级别的任何 zip 文件。

我正在尝试制作的极其简化的代码可能看起来像这样。

.htaccess

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)\.(zip)$ dl.php?filename=$1 [L,QSA]

index.php

//various bits of php code, and this below
<div>
    <a href="1.zip">1</a>
</div>
<div>
    <a href="2.zip">2</a>
</div>
<div>
    <a href="3.zip">3</a>
</div>

dl.php

$file = "../../".$_GET['filename'].".zip";

header('Content-type: application/zip');
header('Content-length: ' . filesize($file));
readfile($file);

die();

我应该这样做,还是有更好的方法?我只是想为一种特定的文件类型执行此操作,在一个特定的页面上可用(例如 index.php,在示例中),所以我并不是真的想为此创建一个完整的路由系统。

【问题讨论】:

  • 当$_GET['filename'] 是../server_secrets 时会发生什么?
  • @MonkeyZeus 有效点。我想我会考虑另一种方法。

标签: php .htaccess open-basedir


【解决方案1】:

假设您的 .htaccess 工作正常,那么您将需要 dl.php 执行类似的操作以确保安全:

$directory = '../../';

// Get the a file listing from the directory which contains the files
foreach(array_diff(scandir($directory), array('..', '.')) as $file)
{
    if(
        strtolower($file) === strtolower($_GET['filename'].'.zip') && // Check each filename against the requested filename in $_GET. Use strtolower() for case-insensitive filesystems
        is_file($directory.$file) // Make sure that this is a file and not a folder
    )
    {
        // Serve it up!
        header('Content-type: application/zip');
        header('Content-length: ' . filesize($directory.$file));
        readfile($directory.$file);
        die();
    }
}

【讨论】:

  • 很好地列出文件。很抱歉这么长时间才回复并接受。
猜你喜欢
  • 2020-04-16
  • 2014-04-17
  • 2019-05-12
  • 2010-12-23
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
相关资源
最近更新 更多