繁体   English   中英

如何检查PHP中是否存在shell命令

[英]How to check if a shell command exists from PHP

我在php中需要这样的东西:

If (!command_exists('makemiracle')) {
  print 'no miracles';
  return FALSE;
}
else {
  // safely call the command knowing that it exists in the host system
  shell_exec('makemiracle');
}

有什么解决方案吗?

在Linux / Mac OS上试试这个:

function command_exist($cmd) {
    $return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
    return !empty($return);
}

然后在代码中使用它:

if (!command_exist('makemiracle')) {
    print 'no miracles';
} else {
    shell_exec('makemiracle');
}

更新:根据@ camilo-martin的建议你可以简单地使用:

if (`which makemiracle`) {
    shell_exec('makemiracle');
}

Windows使用where ,UNIX系统, which允许本地化的命令。 如果找不到命令,两者都将在STDOUT中返回一个空字符串。

对于PHP支持的每个Windows版本,PHP_OS目前都是WINNT。

这是一个便携式解决方案:

/**
 * Determines if a command exists on the current environment
 *
 * @param string $command The command to check
 * @return bool True if the command has been found ; otherwise, false.
 */
function command_exists ($command) {
  $whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which';

  $process = proc_open(
    "$whereIsCommand $command",
    array(
      0 => array("pipe", "r"), //STDIN
      1 => array("pipe", "w"), //STDOUT
      2 => array("pipe", "w"), //STDERR
    ),
    $pipes
  );
  if ($process !== false) {
    $stdout = stream_get_contents($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);

    return $stdout != '';
  }

  return false;
}

您可以使用is_executable来检查它是否可执行,但您需要知道命令的路径,您可以使用which命令来获取它。

独立于平台的解决

function cmd_exists($command)
{
    if (\strtolower(\substr(PHP_OS, 0, 3)) === 'win')
    {
        $fp = \popen("where $command", "r");
        $result = \fgets($fp, 255);
        $exists = ! \preg_match('#Could not find files#', $result);
        \pclose($fp);   
    }
    else # non-Windows
    {
        $fp = \popen("which $command", "r");
        $result = \fgets($fp, 255);
        $exists = ! empty($result);
        \pclose($fp);
    }

    return $exists;
}

基于@jcubic和'应该避免' ,这是我提出的跨平台:

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

基于@xdazz答案,适用于Windows和Linux。 也应该在MacOSX上工作,因为它是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")));
}
function checkIfCommandExists($cmd){
    $prefix = strpos(strtolower(PHP_OS),'win') > -1 ? 'where' : 'which';
    exec("{$prefix} {$cmd}", $output, $returnVal);
    $returnVal !== 0
}

这个是跨平台解决方案,使用“where”和“which”的返回值:)

不是,没有。

即使直接访问shell,您也不知道命令是否存在。 有像wherisfind / -name yourcommand这样的一些技巧,但不是100%保证你可以执行命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM