2

When I was using scandir to scan some specific dir without system permission, I got this output from my php:

<br />
<b>Warning</b>:  scandir(/root): failed to open dir: Permission denied in <b>/var/blog/wordpress/ask.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  scandir(): (errno 13): Permission denied in <b>/var/blog/wordpress/ask.php</b> on line <b>17</b><br />
  1. I don't know why there were 2 similar warning output from php.(I guessed that one is php's and the other is shell's ? Plz tell me.)

  2. I tried to capture this warning(capture the linux errno) to notify my browser client to retype a dir with fully permission.

My code is just like this:

set_error_handler("warning_handler");
scandir ("/root");
restore_error_handler();

// this $errno is not scandir's $errno (Linux $errno)
function warning_handler($errno, $errstr, $errfile, $errline, array $errcontext) {
    // error was suppressed with the @-operator
    if (0 === error_reporting()) {
        return false;
    }
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

But only one warning would be captured(the one without string'errno'). So now I am wondering if there is a method to capture all warnings? Or any other hack methods?

I don't mean that I need a way to try……catch a warning. But there are some situations that I can't fully catch warnings using the method of that.

eisbehr
  • 12,243
  • 7
  • 38
  • 63
Anon
  • 214
  • 1
  • 11
  • Please tell me you're not letting users enter arbitrary paths as user input and relying on nothing more than filesystem permissions to ensure nothing goes wrong. – GordonM Jun 11 '18 at 08:35
  • This doesn’t need any custom error handling … Check the return value of scandir, and if that indicates an error - then go use is_readable to find out if missing permissions were the cause. – CBroe Jun 11 '18 at 08:38
  • 1
    Possible duplicate of [Can I try/catch a warning?](https://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning) – LuFFy Jun 11 '18 at 08:45
  • @CBroe When scandir returns false, how could I figure out whether it is no permission or no such a dir. Both of these are not readble. – Anon Jun 11 '18 at 08:48
  • If it's a command-line script, one warning is printed to stdout and another one its printed to stderr. That's life. – Álvaro González Jun 11 '18 at 09:00

0 Answers0