如何找到mkdir从PHP失败的原因?

问题描述:

PHP的mkdir函数仅返回true和false.问题是它返回false时.

PHP's mkdir function only returns true and false. Problem is when it returns false.

如果在启用错误报告的情况下运行,我会在屏幕上看到错误消息.我还可以在Apache日志中看到错误消息.但是我想获取消息的文本并对其进行其他处理(例如,通过IM发送给自己).如何获取错误文本?

If I'm running with error reporting enabled, I see the error message on the screen. I can also see the error message in the Apache log. But I'd like to grab the text of the message and do something else with it (ex. send to myself via IM). How do I get the error text?

更新:按照艾曼的想法,我来到了这里:

Update: Following Ayman's idea, I came to this:

function error_handler($errno, $errstr) {
    global $last_error;
    $last_error = $errstr;
}

set_error_handler('error_handler');
if (!mkdir('/somedir'))
    echo "MKDIR failed, reason: $last_error\n";
restore_error_handler();

但是,我不喜欢它,因为它使用了全局变量.有更清洁的解决方案的想法吗?

However, I don't like it because it uses global variable. Any idea for a cleaner solution?

您可以 :

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