Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How to know if server allows shell_exec

On some servers, PHP is not allowed to run shell commands via shell_exec. How can I detect if current server allows running shell commands via PHP or not? How can I enable shell commands execution via PHP?

like image 837
arxoft Avatar asked Feb 05 '14 15:02

arxoft


People also ask

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.

What is the difference between exec and Shell_exec?

The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.


1 Answers

First check that it's callable and then that it's not disabled:

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

This general approach works for any built in function, so you can genericize 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 to use stripos, because PHP function names are case insensitive.

like image 83
bishop Avatar answered Sep 30 '22 18:09

bishop