【相关学习推荐:php图文教程】
这里演示一些普通的shell命令
string system ( string $command [, int &$return_var ] )
string exec ( string $command [, array &$output [, int &$return_var ]] )
void passthru ( string $command [, int &$return_var ] )
注意的是:这三个函数在默认的情况下,都是被禁止了的,如果要使用这几个函数,就要先修改PHP的配置文件PHP.ini,查找关键字disable_functions,将这一项中的这几个函数名删除掉,然后注意重启apache。
首先看一下system()和passthru()两个功能类似,可以互换:
<?PHP $shell = ls -la; echo <pre>; system($shell, $status); echo </pre>; //注意shell命令的执行结果和执行返回的状态值的对应关系 $shell = <font color='red'>$shell</font>; if( $status ){ echo shell命令{$shell}执行失败; } else { echo shell命令{$shell}成功执行; } ?>
执行结果如下:
注意,system()会将shell命令执行之后,立马显示结果,这一点会比较不方便,因为我们有时候不需要结果立马输出,甚至不需要输出,于是可以用到exec()
exec()的使用示例:
<?PHP $shell = ls -la; exec($shell, $result, $status); $shell = <font color='red'>$shell</font>; echo <pre>; if( $status ){ echo shell命令{$shell}执行失败; } else { echo shell命令{$shell}成功执行, 结果如下<hr>; print_r( $result ); } echo </pre>; ?>
运行结果如下:
相关学习推荐:php编程(视频)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。