Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve PHP exec() error responses?

Tags:

shell

php

exec

Below is the command I tried executing, without success:

exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess'); 

When you add a die() at the end, it catches that there's an error:

exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess') or die('what?!'); 

For the above exec() statement, a permissions problem is causing the error, but PHP isn't displaying it. How do you display from PHP what error is occurring?

like image 394
Matt Avatar asked Dec 06 '08 02:12

Matt


People also ask

What does Exec return in PHP?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.

Does PHP exec wait until finished?

PHP exec will wait until the execution of the called program is finished, before processing the next line, unless you use & at the end of the string to run the program in background.

How do you check if exec is enabled in PHP?

php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.


1 Answers

The following code will capture both the normal output (from StdOut) and the error output (from SdtErr).

exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess 2>&1',$output); var_dump($output); 
like image 124
Amaynut Avatar answered Sep 22 '22 13:09

Amaynut