【问题标题】:block access to file while using opendir/openfile etc在使用 opendir/openfile 等时阻止对文件的访问
【发布时间】:2013-08-07 14:23:00
【问题描述】:

我想拒绝对 file_exists/opendir/openfile 等函数的文件 data.xml 的访问

我的文件 test.php 包含:

$path="./mydata.html";
if ($handle = opendir($path)) {

}

closedir($handle);

如何在不阻塞函数的情况下使其不可读?有可能吗?

  • .htaccess?
  • 我应该通过不同用户设置的 chmod 阻止文件而不是执行 php 脚本吗?
  • 其他方式?

谢谢

【问题讨论】:

    标签: php file .htaccess block chmod


    【解决方案1】:

    最简单的方法是编写一个函数来检查文件路径,并在您接受用户数据时或在每个fopen()/opendir()/file_get_contents() 之前调用(即)sanitize_filepath($path)。在sanitize_filepath() 函数中,在PATH_SEPARATOR 上分解$path 并使用in_array() 根据您提供的路径检查您的内部黑名单。

    如果你不看继续,则返回 false 或抛出异常,如果你看,则返回文件路径或 true。

    阻止通过 .htaccess 访问仅意味着我无法转到 http://example.com/your/website/data.xml 并查看该文件。如果我请求 http://example.com/your/website/script.php?file=data.xml,我仍然可以通过利用您的 PHP 脚本访问该文件,假设 script.php 是 <?= echo file_get_contents($_GET['file']); ?>。 (这显然是一个无用且不安全的脚本,但它是一个示例。)

    通过 chmodding 权限来阻止访问可能会阻止 PHP 读取文件。如果您不需要阅读该文件,则它不应该位于 PHP 可以访问它的位置——将它移到您的 webroot 之外。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多