Question

rmdir() displays a few warnings like the dir does not exist, or permissions did not allow. How can I capture which reason for failure and react to it?

Was it helpful?

Solution

rmdir does not throw Exception so you cannot catch them with try/catch. What you can do is use error_get_last function to do what you need.

Try something like this:

if (!@rmdir('/root')) {
    $error = error_get_last();

    if (preg_match('/something/', $error['message'])) {
        // do something
    } elseif (preg_match('/somethingelse/', $error['message'])) {
        // do something
    }
}

OTHER TIPS

You can check beforehand if you are allowed to do some kind of action like file_exists() and is_ dir() to check if a directory exists and fileperms() or just is_ writable() to check if you can write a directory.

You can also try to "catch" the error like with exceptions. you can specify a custom error handler, but this seems to be a bit overkill.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top