PHP exec - check if enabled or disabled
Solution 1
if(function_exists('exec')) {
echo "exec is enabled";
}
Solution 2
This will check if the function actually works (permissions, rights, etc):
if(@exec('echo EXEC') == 'EXEC'){
echo 'exec works';
}
Solution 3
ini_get('disable_functions')
What you actually want to do is use ini_get('disable_functions')
to find out if it is available to you:
<?php
function exec_enabled() {
$disabled = explode(',', ini_get('disable_functions'));
return !in_array('exec', $disabled);
}
?>
Answered on stackoverflow here: Check if "exec" is disabled, Which actually seems to come from the PHP Man page: http://php.net/manual/en/function.exec.php#97187
Path
If the above returns true (you can use exec()), but PHP can still not trigger the script there is a good chance that you have a path issue for that script, test this by doing:
print exec('which bash');
and then try
print exec('which ogr2ogr');
Solution 4
This will check that exec is available and enabled BEFORE trying to run it. If you run exec() and the function does not exist or is disabled a warning will be generated. Depending on the server settings that may render to the browser and will almost-always write a line to a log file = performance hit.
// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$exec_enabled =
function_exists('exec') &&
!in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
strtolower(ini_get('safe_mode')) != 1
;
if($exec_enabled) { exec('blah'); }
Solution 5
It's a bit tricky to find exec
function available until unless checking all possibilities
1.function_exist('exec')
2.Scanning through ini_get('disabled_functions)
3.Checking safe_mode
enabled
function is_shell_exec_available() {
if (in_array(strtolower(ini_get('safe_mode')), array('on', '1'), true) || (!function_exists('exec'))) {
return false;
}
$disabled_functions = explode(',', ini_get('disable_functions'));
$exec_enabled = !in_array('exec', $disabled_functions);
return ($exec_enabled) ? true : false;
}
This function never throws warnings unless ini_get
function not disabled.
Adrian M.
Updated on January 29, 2021Comments
-
Adrian M. over 3 years
Is there a way to check in a php script if
exec()
is enabled or disabled on a server?