How to check if a shell command exists from PHP

后端 未结 7 971
孤街浪徒
孤街浪徒 2020-12-25 11:07

I need something like this in php:

If (!command_exists(\'makemiracle\')) {
  print \'no miracles\';
  return FALSE;
}
else {
  // safely call the command kno         


        
7条回答
  •  礼貌的吻别
    2020-12-25 11:42

    Based on @jcubic and that 'which' should be avoided, this is the cross platform I came up with:

    function verifyCommand($command) :bool {
      $windows = strpos(PHP_OS, 'WIN') === 0;
      $test = $windows ? 'where' : 'command -v';
      return is_executable(trim(shell_exec("$test $command")));
    }
    

提交回复
热议问题