0

When I run this code:

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

then I get the error:

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

and the script crashes.

I know hot to fix this, but how can I check if this will happen before it will happen?

I tried this:

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

but the condition is always true

  • Does this answer your question? [Check if open\_basedir restriction is in effect](https://stackoverflow.com/questions/21427211/check-if-open-basedir-restriction-is-in-effect) – Nico Haase Oct 03 '21 at 14:42
  • Or this? https://stackoverflow.com/questions/12984288/check-if-a-path-will-fail-due-to-open-basedir – Nico Haase Oct 03 '21 at 14:42
  • Doesn't solve your problem but please get rid of `@`. You don't want to supress any bugs. You want to fix them. – B001ᛦ Oct 03 '21 at 14:50

1 Answers1

0

I would expect the is_readable() function to not return true if the file is outside an open_base_dir constraint as well as checking for the existence of the file and appropriate permissions.

If you explicitly want to check for an open base for constraint, compare ini_get('open_basedir') with realpath($yourfile)

update

It's not rocket science (but you should still test this)....

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;
}
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • What does compare ini_get('open_basedir') with realpath($yourfile) mean? Can you show how? –  Oct 03 '21 at 15:22