Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a reason when mkdir fails from PHP?

Tags:

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

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?

like image 288
Milan Babuškov Avatar asked May 29 '09 18:05

Milan Babuškov


People also ask

How to check mkdir error php?

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();

Can mkdir fail?

The mkdir() function shall fail if: [EACCES] Search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be created.

What is mkdir in PHP?

The mkdir() function creates a directory specified by a pathname.


1 Answers

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

if (!@mkdir($dir)) {     $error = error_get_last();     echo $error['message']; } 
like image 168
soulmerge Avatar answered Oct 02 '22 16:10

soulmerge