/**
* [command_exec description] 命令执行
* @param string $command [description] 要执行的命令语句
* @return [type] [description] 返回对应执行结果,运行状态
* @author lianggc 2016-10-13
*/
function command_exec($command=''){
$output='';
$return_state='';
if(function_exists('system')){//检测system 是否存在
ob_start();
system($command , $return_state);
$output = ob_get_contents();
ob_end_clean();
}elseif(function_exists('passthru')){//passthru 是否存在
ob_start();
passthru($command , $return_state);
$output = ob_get_contents();
ob_end_clean();
}elseif(function_exists('exec')){//shell_exec 是否存在
exec($command , $output , $return_state);
$output = implode("\n" , $output);
}elseif(function_exists('shell_exec')){//shell_exec 是否存在
$output = shell_exec($command) ;
}else{
//上面的函数都不可使用,一般情况下php.ini屏蔽这些函数。
//低版本的php会有安全模式进行屏蔽
$output = '无法运行此命令!';
$return_state = 1;
}
return [
'output' => $output ,
'status' => $return_state,
];
}
echo "<pre>";
print_r(command_exec('dir'));die;
php如何执行操作系统命令
最新推荐文章于 2020-03-07 17:27:57 发布