如何找到mkdir从PHP失败的原因? [英] How to find a reason when mkdir fails from PHP?

查看:231
本文介绍了如何找到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'];
}

这篇关于如何找到mkdir从PHP失败的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆