How to check if a shell command exists from PHP

后端 未结 7 1001
孤街浪徒
孤街浪徒 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 @xdazz answer, works on Windows and Linux. Should also work on MacOSX too, since it's unix.

    function is_windows() {
      return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
    }
    
    function command_exists($command) {
        $test = is_windows() ? "where" : "which";
        return is_executable(trim(shell_exec("$test $command")));
    }
    
    0 讨论(0)
提交回复
热议问题