Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1838923
  • 博文数量: 333
  • 博客积分: 10791
  • 博客等级: 上将
  • 技术积分: 4314
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 07:39
文章分类

全部博文(333)

文章存档

2015年(1)

2011年(116)

2010年(187)

2009年(25)

2008年(3)

2007年(1)

分类: Python/Ruby

2010-12-31 15:42:38

一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。介绍以下6个函数。

一、exec函数

<?php
$test = "ls /tmp/test"; //ls是linux下的查目录,文件的命令
exec($test,$array); //执行命令
print_r($array);
?>


二、system函数

<?php
$test = "ls /tmp/test";
$last = system($test);
print "last: $last";
?>


三、passthru函数

<?php
$test = "ls /tmp/test";
passthru($test);
?>


四、popen函数

<?php
$test = "ls /tmp/test";
$fp = popen($test,"r"); //popen打一个进程通道

while (!feof($fp)) { //从通道里面取得东西
  $out = fgets($fp, 4096);
    echo $out; //打印出来
} 


pclose($fp);
?>


五、proc_open函数

<?php
$test = "ls /tmp/test";
$arrayarray = 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);
?>


六、shell_exec函数

<?php
$test = "ls /tmp/test";
$out = shell_exec($test);
echo $out;
?>


阅读(1130) | 评论(0) | 转发(0) |
0

上一篇:Quixote笔记

下一篇:Ubuntu网卡驱动

给主人留下些什么吧!~~