Detecting the rmdir error with php

自作多情 提交于 2020-01-30 05:50:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!