【问题标题】:PHP - open_basedir restriction in effect check if a PHP extension can read a filePHP - open_basedir 限制有效检查 PHP 扩展是否可以读取文件
【发布时间】:2021-10-03 14:31:17
【问题描述】:

当我运行这段代码时:

$img = new Imagick('ttt.jpg');
$quality = $img->getImageCompressionQuality();
echo $quality;

然后我得到错误:

PHP Fatal error: Uncaught ImagickException: open_basedir restriction in effect. File(ttt.jpg) is not within the allowed path(s)

脚本崩溃了。

我知道要解决这个问题,但我如何才能在它发生之前检查它是否会发生?

我试过这个:

$readable = @is_readable('ttt.jpg');
if(!$readable) {
    echo 'not readable';
}else{
    $img = new Imagick('ttt.jpg');
    $quality = $img->getImageCompressionQuality();
    echo $quality; 
}

但条件始终为真

【问题讨论】:

标签: php


【解决方案1】:

如果文件超出 open_base_dir 约束以及检查文件是否存在和适当的权限,我希望 is_readable() 函数不会返回 true。

如果您明确要检查约束的开放基数,请将ini_get('open_basedir')realpath($yourfile) 进行比较

更新

这不是火箭科学(但你还是应该测试一下)......

function check_if_obd_permitted($file)
{
   $obd=ini_get('open_basedir');
   if (!$obd) return true;
   if (substr(realpath($file), 0, strlen($obd))==$obd) return true;
   return false;
}

【讨论】:

  • 比较 ini_get('open_basedir') 和 realpath($yourfile) 是什么意思?你能告诉我怎么做吗?
猜你喜欢
  • 2014-02-21
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 2014-09-27
  • 2013-06-23
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多