How to find a reason when mkdir fails from PHP?

我怕爱的太早我们不能终老 提交于 2019-11-27 22:51:29

You can suppress the warning and make use of error_get_last():

if (!@mkdir($dir)) {
    $error = error_get_last();
    echo $error['message'];
}

You could use exceptions:

Setup some code like so:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

And then just do:

try {
   mkdir('/somedir');
} catch(ErrorException $ex) {
   echo "Error: " . $ex->getMessage();
}

That should do what you want.

If you want to preserve the php error handler, then after that try catch block, just call:

restore_error_handler()

I use something like the following:

if(! @mkdir('$fileLocation', 0777, $recursive = true)){
    $mkdirErrorArray = error_get_last();
    throw new Exception('cant create directory ' .$mkdirErrorArray['message'], 1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!