问题
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?
回答1:
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
}
}
回答2:
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.
来源:https://stackoverflow.com/questions/9438866/detecting-the-rmdir-error-with-php