PHP执行linux命令6个函数代码实例
时间:2022-01-13
来源:互联网
一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec。其实有很多的,结合手册内容,介绍以下6个函数。
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
我能发现的就这几个函数,能执行linux下的命令,我想应当还有吧,欢迎大家补充。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP爱好者。
-
CSS3阴影效果属性 实现阴影效果的方法(附代码) 时间:2025-08-14
-
什么是Amaze UI Amaze UI使用教程 时间:2025-08-14
-
HTML中option标签详解(定义、属性、示例代码) 时间:2025-08-14
-
Android中翻转视图组件ViewFlipper使用详解(基本使用方式、常用方法、示例应用) 时间:2025-08-14
-
Linux重启命令有哪些?多种方式适用不同的场景和需求 时间:2025-08-14
-
动态ip和静态ip是什么意思 动态IP和静态IP的区别 时间:2025-08-13
今日更新
-
揭秘青山精神病院爆火网络梗-搞笑网友自称病友的幽默自嘲文化
阅读:18
-
三国杀梗是什么梗-揭秘游戏中的爆笑名场面
阅读:18
-
什么帝是什么梗-指网络红人因夸张表现被网友封帝
阅读:18
-
神梗梗是网络热梗的升级版-搞笑又魔性让人忍不住模仿
阅读:18
-
四角形的星星是什么梗-揭秘网络热词背后的趣味含义
阅读:18
-
铁柱是什么梗揭秘网络热词背后的搞笑故事-铁柱原来是指这个让人笑喷的梗
阅读:18
-
王者沉默的梗是什么梗呀-揭秘王者荣耀玩家集体失声的搞笑真相
阅读:18
-
我黑切呢是什么梗-揭秘游戏圈经典爆笑名场面
阅读:18
-
系鞋带是什么梗揭秘网络流行暗号-系鞋带背后的隐藏含义你知道吗
阅读:18
-
小学生梗是什么梗-揭秘网络流行语背后的趣味故事
阅读:18