教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP中system,exec,passthru函数用法

PHP中system,exec,passthru函数用法

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供PHP中system,exec,passthru函数用法等资源,欢迎您收藏本站,我们将为您提供最新的PHP中system,exec,passthru函数用法资源
本文章来给各位同学介绍关于在php中system,exec,passthru函数的一些使用方法,各位同学不防进入参考。


php提供了system(),exec(),passthru()这几个函数来调用外部的命令。

他们的区别 :

system() 输出并返回最后一行shell结果。
exec() 不输出结果,返回最后一行shell结果,所有结果可以保存到一个返回的数组里面。
passthru() 只调用命令,把命令的运行结果原样地直接输出到标准输出设备上。
相同点:都可以获得命令执行的状态码


ystem()、exec()、shell_exec() 官方文件?明如下:

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 )
ystem()、exec()、shell_exec() 官方文件说明如下:

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: 回传内容都会存於此变数中(储存成纯文字内容), 不会直接秀在页面上

在PHP中调用外部命令,可以用如下三种方法来实现:

1) 用PHP提供的专门函数
PHP提供共了3个专门的执行外部命令的函数:system(),exec(),passthru()。

system()

原型:string system (string command [, int return_var])
system()函数很其它语言中的差不多,它执行给定的命令,输出和返回结果。第二个参数是可选的,用来得到命令执行后的状态码。

返回结果

        成功返回0, 
        失败(命令不存在等原因)   返回   非0值

例子: system("/usr/local/bin/webalizer/webalizer");

exec()

原型:string exec (string command [, string array [, int return_var]])
exec ()函数与system()类似,也执行给定的命令,但不输出结果,而是返回结果的最后一行。虽然它只返回命令结果的最后一行,但用第二个参数array 可以得到完整的结果,方法是把结果逐行追加到array的结尾处。所以如果array不是空的,在调用之前最好用unset()最它清掉。只有指定了第二 个参数时,才可以用第三个参数,用来取得命令执行的状态码。

例子:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7429')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7429>exec("/bin/ls -l");
exec("/bin/ls -l", $res);
exec("/bin/ls -l", $res, $rc);

passthru()

原型:void passthru (string command [, int return_var])
passthru ()只调用命令,不返回任何结果,但把命令的运行结果原样地直接输出到标准输出设备上。所以passthru()函数经常用来调用象pbmplus (Unix下的一个处理图片的工具,输出二进制的原始图片的流)这样的程序。同样它也可以得到命令执行的状态码。

例子:
header("Content-type: image/gif");
passthru("./ppmtogif hunte.ppm");

2) 用popen()函数打开进程

上面的方法只能简单地执行命令,却不能与命令交互。但有些时候必须向命令输入一些东西,如在增加Linux的系统用户时,要调用su来把当前用户换到 root才行,而su命令必须要在命令行上输入root的密码。这种情况下,用上面提到的方法显然是不行的。
popen ()函数打开一个进程管道来执行给定的命令,返回一个文件句柄。既然返回的是一个文件句柄,那么就可以对它读和写了。在PHP3中,对这种句柄只能做单一 的操作模式,要么写,要么读;从PHP4开始,可以同时读和写了。除非这个句柄是以一种模式(读或写)打开的,否则必须调用pclose()函数来关闭它。

例子1:
$fp=popen("/bin/ls -l", "r");

例子2 :
/* PHP中如何增加一个系统用户
下面是一段例程,增加一个名字为james的用户,
root密码是 verygood。仅供参考

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9440')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9440>*/
$sucommand = "su --login root --command";
$useradd = "useradd ";
$rootpasswd = "verygood";
$user = "james";
$user_add = sprintf("%s "%s %s"",$sucommand,$useradd,$user);
$fp = @popen($user_add,"w");
@fputs($fp,$rootpasswd);
@pclose($fp);
?>

3) 用反撇号(`,也就是键盘上ESC键下面的那个,和~在同一个上面)
这个方法以前没有归入PHP的文档,是作为一个秘技存在的。方法很简单,用两个反撇号把要执行的命令括起来作为一个表达式,这个表达式的值就是命令执行的结果。如:
$res='/bin/ls -l';
echo '
'.$res.'
';

这个脚本的输出就象:
hunte.gif
hunte.ppm
jpg.htm
jpg.jpg
passthru.php


范例程式

由此范例执行一次就比较容易理解. (请建立一个目录, 随便放两个档案, 再将此程式放置执行)

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8481')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8481>

<?php
echo "nsystem";
$last_line = system('ls', $return_var);
echo "nreturn_var:";
print_r($return_var);
echo "nlast_line:";
print_r($last_line);

echo "nnexec";
exec('ls', $output, $return_var);
echo "nreturn_var:";
print_r($return_var);
echo "noutput:";
print_r($output);

echo "nnshell_exec";
$output = shell_exec('ls');
echo "noutput:";
print_r($output);
?>

*/

 

?>

您可能感兴趣的文章:
php调用外部shell的方法总结
PHP中system,exec,passthru函数用法
PHP执行系统命令的有几个常用的函数
PHP 中执行系统外部命令
PHP出现Warning: scandir()问题的解决方法
php函数system
php 执行系统外部命令的方法详解
PHP调用linux命令详细说明
php执行linux系统命令的方法介绍
php无法调用外部命令时的处理方法

[关闭]
~ ~