PHP - How to find out if the server allows shell_exec

On some servers, PHP cannot run shell commands through shell_exec. How can I determine if the current server allows shell commands to run through PHP or not? How to enable shell command execution through PHP?

+7
php shell shell-exec
source share
2 answers

First check that it is called and then it is not disconnected:

is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 'shell_exec'); 

This general approach works for any built-in function, so you can generalize it:

 function isEnabled($func) { return is_callable($func) && false === stripos(ini_get('disable_functions'), $func); } if (isEnabled('shell_exec')) { shell_exec('echo "hello world"'); } 

Note for using stripos because PHP function names are not case sensitive.

+9
source share

You can check the availability of the function itself:

 if(function_exists('shell_exec')) { echo "exec is enabled"; } 
By the way, is there a special requirement to use shell_exec rather than exex?

php.net

 Note: This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required. 

EDIT NO. 1

As DanFromGermany pointed out, you are probably checking to see if it is executable. Something like this would do it

 if(shell_exec('echo foobar') == 'foobar'){ echo 'shell_exec works'; } 

EDIT No. 2

If the above example may give warnings, you can do it in a more appropriate way. Just see this SO answer .

+2
source share

All Articles