PHP中exec与shell_exec函数区别

这两个函数都是可以执行Linux命令函数,不同的是获取返回结果不一样,exec只能获取最后一行数据,shell_exec则可以获取全部数据。

假如脚本路径下有如下文件

-bash-4.1# ll
总用量 12
-rw-rw-r--. 1 www web 133  7月 16 15:00 a.php
-rw-r--r--. 1 lee web  59  2月 29 17:05 b.php
-rw-r--r--. 1 lee web  81  3月  8 17:00 c.php

exec例子

<?php
/**
 * exec与shell_exec的区别
 * 琼台博客 www.qttc.net
 */
$data = exec('/bin/ls -l');
print_r($data);
?>

Output

-rw-r--r--. 1 lee web  81 Mar  8 17:00 c.php

shell_exec例子

<?php
/**
 * exec与shell_exec的区别
 * 琼台博客 www.qttc.net
 */
$data = shell_exec('/bin/ls -l');
print_r($data);
?>

Output

total 12
-rw-rw-r--. 1 www web 139 Jul 16  2012 a.php
-rw-r--r--. 1 lee web  59 Feb 29 17:05 b.php
-rw-r--r--. 1 lee web  81 Mar  8 17:00 c.php

所以平时使用exec函数的童鞋注意了,如果你需要获取全部返回信息,就应该使用shell_exec函数,当然,如果命令执行结果只有一行返回信息,那么使用哪个都无所谓。

分享

TITLE: PHP中exec与shell_exec函数区别

LINK: https://www.qttc.net/132-php-shell-exec.html

NOTE: 原创内容,转载请注明出自琼台博客