在PHP函数如何运行Linux系统代码?这六个函数要知
Admin 2021-04-23 群英技术资讯 1769 次浏览
在开发过程中,有时候我们会需要在PHP函数在运行Linux系统代码,那么我们就会使用到这些函数,shell_exec函数,exec函数,system函数,passthru函数,popen函数,proc_open函数。下面我们就来详细了解下PHP执行linux命令可能会用到的六个函数。
1,exec函数
<?php
$test = "ls /tmp/test"; //ls是linux下的查目录,文件的命令
exec($test,$array); //执行命令
print_r($array);
?>
返回结果如下:
[root@krlcgcms01 shell]# php ./exec.php
Array
(
[0] => 1001.log
[1] => 10.log
[2] => 10.tar.gz
[3] => aaa.tar.gz
[4] => mytest
[5] => test1101
[6] => test1102
[7] => weblog_2010_09
)
2,system函数
<?php
$test = "ls /tmp/test";
$last = system($test);
print "last: $last\n";
?>
返回结果:
[root@krlcgcms01 shell]# php system.php
1001.log
10.log
10.tar.gz
aaa.tar.gz
mytest
test1101
test1102
weblog_2010_09
last:weblog_2010_09
3,passthru函数
<?php
$test = "ls /tmp/test";
passthru($test);
?>
4,popen函数
<?php
$test = "ls /tmp/test";
$fp = popen($test,"r"); //popen打一个进程通道
while (!feof($fp)) { //从通道里面取得东西
$out = fgets($fp, 4096);
echo $out; //打印出来
}
pclose($fp);
?>
5,proc_open函数
<?php
$test = "ls /tmp/test";
$array = array(
array("pipe","r"), //标准输入
array("pipe","w"), //标准输出内容
array("pipe","w") //标准输出错误
);
$fp = proc_open($test,$array,$pipes); //打开一个进程通道
echo stream_get_contents($pipes[1]); //为什么是$pipes[1],因为1是输出内容
proc_close($fp);
?>
6,shell_exec函数
<?php
$test = "ls /tmp/test";
$out = shell_exec($test);
echo $out;
?>
popen,passthru,proc_open,shell_exec的返回结果如下:
[root@krlcgcms01 shell]# php test.php
1001.log
10.log
10.tar.gz
aaa.tar.gz
mytest
test1101
test1102
weblog_2010_09
以上就是PHP执行linux命令六个函数的介绍,通过上述函数,PHP就可以执行Linux命令啦,希望上述内容对大家学习有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
自PHP5.4起可用callable类型指定回调类型callback。本文档基于同样理由使用callback类型信息。一些函数如call_user_func()或usort()可以接受用户自定义的回调...
swoole不能访问的解决办法:首先使用命令“yum install -y openssl”安装openssl;然后安装swoole;接着通命令“php-config --enable-openssl”进行编译即可正常访问。
什么是命名空间?从广义上来说,命名空间是一种封装事务的方法。PHP命名空间(namespace)是在PHP 5.3中加入的,如果你学过C#和Java,那命名空间就不算什么新事物。不过在PHP当中还是有着相当重要的意义。
swoole安装:1、PECL安装,先使用“apt-get install libpcre3 libpcre3-dev”命令安装依赖包。2、使用“pecl install swoole”命令安装swoole。3、修改PHP配置php.in。
用两个栈,出栈时全部出到第二个栈中,然后从第二个栈中出栈就能模拟队列队列操作类主要使用array_push()入队array_shift()出队(栈的实现类似,使用array_push()入栈,array_pop出栈)<?php/**PHP实现队列操作类*/classqueueOp{/**队尾入队*Retu
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008