在PHP函数如何运行Linux系统代码?这六个函数要知
Admin 2021-04-23 群英技术资讯 1480 次浏览
在开发过程中,有时候我们会需要在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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章给大家带来了Laravel目录结构与配置的相关知识,Laravel的目录结构相对来说在初始状态下会更丰富一些,除了传统的控制器之外,也帮我们准备好了脚本、中间件之类的代码文件的目录,基本上是可以达到上手直接使用的,希望对大家有帮助。
在thinkphp中,I方法的命名来自于“Input”,用于更加安全和方便的获取系统输入变量,语法为“I('变量类型.变量名',['默认值'],['过滤方法'])”;其中变量类型不区分大小写,而变量名严格区分大小写。
这篇文章主要介绍了PHP 代码简洁之道(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在之前的文章中给大家带来了《PHP回调函数怎样使用(实例详解)》,这篇文章中详细的介绍了PHP中的回调函数的相关知识,本篇文章来看一看PHP函数中基本的匿名函数有哪些值得关注的知识点。希望能够帮助到大家!
今天小编就为大家分享一篇laravel 出现command not found问题的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008