开发者

PHP: run "mkdir" and get errno

开发者 https://www.devze.com 2023-02-17 05:36 出处:网络
I need to execute mkdir command (e.g. via PHP\'s exec command). How do I access stand开发者_如何学Goard error (e.g. EACCES, see: http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html). S

I need to execute mkdir command (e.g. via PHP's exec command). How do I access stand开发者_如何学Goard error (e.g. EACCES, see: http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html). Suggestions? Thanks.

UPDATE:

I added 2>&1 to my command:

$command = "sudo mkdir /home/test 2>&1";
$output  = array();
$return  = 0;
exec($command, $output, $return);

Without it, I used to get either 0 (success) or -1 (error). Now, I get a 1 during one of my tests -- and I think it's because the directory I am trying to create already exist. It would seem that 1 maps to EEXIST. How do I map the rest of the errno?


You cannot.

You want to catch the error of the mkdir function via errno, but your call to PHP's exec() means that you will instead deal with the exit code of the external program mkdir.

It is the difference between:

http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html (which you cite)
and
http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html (which you exec)


You're probably looking for proc_open, which gives you the ability to work directly with stdin, stdout and stderr as PHP streams, meaning you can use the normal file reading and writing functions on them.

Please be sure to read more details about the proc_ family tree on the proc_close and proc_get_status pages.

You might have more luck working with the text of the error as provided to stderr instead of working with exit codes.

0

精彩评论

暂无评论...
验证码 换一张
取 消