PHP 执行 Shell 命令

主要有 exec() shell_exec() system() popen()

exec()

1
string exec(string $command [, array $output [, int $return_var ]] )

返回最后一条结果。

如果使用了 output 参数,shell 命令的每行结果会填充到该数组中。

shell_exec()

1
string shell_exec(string $cmd)

字符串 形式返回执行的全部结果。

system()

1
string system(string $command [, int $return_value])

输出全部结果。

成功则返回命令输出的最后一行, 失败则返回 FALSE

popen()

1
2
3
4
5
6
7
8
9
$ph=popen('ls','r');

// 返回类型为资源

// 读取命令执行结果

var_dump(stream_get_contents($ph));

pclose($ph);

反引号

反引号不能在双引号字符串中使用。

1
echo `ls`;

其他

请查看:http://php.net/manual/zh/ref.exec.php

0%