Search code examples
phprmdir

Detecting the rmdir error with php


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?


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
        }
    }