PHP-如何知道服务器是否允许shell_exec
问题描述:
在某些服务器上,不允许PHP通过shell_exec运行shell命令.如何检测当前服务器是否允许通过PHP运行Shell命令?如何通过PHP启用Shell命令执行?
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?
答
首先检查它是否可调用,然后再将其禁用:
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"');
}
请注意使用stripos
,因为PHP函数名称不区分大小写.
Note to use stripos
, because PHP function names are case insensitive.