php可以通过 system()、exec()、shell_exec()执行系统命令,但是各有区别,PHP官方文件说明如下:
system — Execute an external program and display the output
string system ( string $command [, int &$return_var ] )
exec — Execute an external program
string exec ( string $command [, array &$output [, int &$return_var ]] )
shell_exec — Execute command via shell and return the complete output as a string
string shell_exec ( string $cmd )
一般系统会有两种输出, 一种是系统状态(return code), 一种是文字(output string), 这三个 Function 主要就是这些输出的差异.
system()
$last_line = system(‘ls’, $return_var);
system() 会将输出内容直接输出, 所以若于网页, 会将所有内容都显示于页面上.
$last_line: 只能取得最后一行的内容
$return_var: 取得系统状态码
exec()
exec(‘ls’, $output, $return_var);
$output: 返回内容都会存在此变量中(储存成数组), 不会直接显示在页面上.
$return_var: 取得系统状态码
shell_exec()
$output = shell_exec(‘ls’);
$output: 返回内容都会存在此变量中(纯文本内容), 不会直接显示在页面上.