【相关学习推荐:php图文教程】
这里演示一些普通的shell命令
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。
立即学习“PHP免费学习笔记(深入)”;
首先看一下system()和passthru()两个功能类似,可以互换:
<?php $shell = "ls -la"; echo "<pre class="brush:php;toolbar:false">"; system($shell, $status); echo "
执行结果如下:
注意,system()会将shell命令执行之后,立马显示结果,这一点会比较不方便,因为我们有时候不需要结果立马输出,甚至不需要输出,于是可以用到exec()
exec()的使用示例:
<?php $shell = "ls -la"; exec($shell, $result, $status); $shell = "<font color='red'>$shell</font>"; echo "<pre class="brush:php;toolbar:false">"; if( $status ){ echo "shell命令{$shell}执行失败"; } else { echo "shell命令{$shell}成功执行, 结果如下<hr>"; print_r( $result ); } echo "
运行结果如下:
相关学习推荐:php编程(视频)
以上就是一起看看PHP执行普通shell命令流程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号