PHP执行linux系统命令时要注意:

    1、执行命令的权限:运行PHP的这个服务的用户来执行系统命令的用户。如运行php的服务是php-fpm,而运行php-fpm的用户是apache,那么用php来执行系统命令的时候使用的就是apache用户。

    2、执行结果权限:如果执行系统命令需要生成文件,那么这个apache用户要拥有这个目录的写权限。

system:

// string system ( string $command [, int &$return_var ] )
system('ls -l', $retval);// 直接输出执行输出结果,是边执行边输出
if($retval == 0) {
    echo 'success';
}
dump($res);


exec:

$command = "ls -al";

//string exec ( string command , [array &output , [int &return_var]] )
// 返回之后一行执行的结果
exec($command, $retval, $status);

// 这是一个数组,包含执行输出的每一行
var_dump($retval);

if( $status == 0 ){
    echo "execute success\n";
}else{
    echo "execute fail\n";
}


shell_exec:

shell_exec('ls'); // 直接输出结果
<?php
    echo `ls`;  // 同反撇号
?>


passthru:

// void passthru ( string $command [, int &$return_var ] )
//同system,直接输出结果,可以输出图像
passthru('ls -l', $retval);


escapeshellcmd:

转义shell元字符,作为system或exec的参数使用

$command = './configure '.$_POST['configure_options'];

$escaped_command = escapeshellcmd($command);
 
system($escaped_command);