一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数。以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec。其实有很多的,结合手册内容,介绍以下6个函数。

1,exec函数

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

返回结果如下:

  1. [root@krlcgcms01 shell]# php ./exec.php
  2. Array
  3. (
  4. [0] => 1001.log
  5. [1] => 10.log
  6. [2] => 10.tar.gz
  7. [3] => aaa.tar.gz
  8. [4] => mytest
  9. [5] => test1101
  10. [6] => test1102
  11. [7] => weblog_2010_09
  12. )

2,system函数

  1. <?php
  2. $test = "ls /tmp/test";
  3. $last = system($test);
  4. print "last: $last\n";
  5. ?>

返回结果:

  1. [root@krlcgcms01 shell]# php system.php
  2. 1001.log
  3. 10.log
  4. 10.tar.gz
  5. aaa.tar.gz
  6. mytest
  7. test1101
  8. test1102
  9. weblog_2010_09
  10. last:weblog_2010_09

3,passthru函数

  1. <?php
  2. $test = "ls /tmp/test";
  3. passthru($test);
  4. ?>

4,popen函数

  1. <?php
  2. $test = "ls /tmp/test";
  3. $fp = popen($test,"r");  //popen打一个进程通道
  4. while (!feof($fp)) {      //从通道里面取得东西
  5. $out = fgets($fp, 4096);
  6. echo  $out;         //打印出来
  7. }
  8. pclose($fp);
  9. ?>

5,proc_open函数

  1. <?php
  2. $test = "ls /tmp/test";
  3. $array =   array(
  4. array("pipe","r"),   //标准输入
  5. array("pipe","w"),   //标准输出内容
  6. array("pipe","w")    //标准输出错误
  7. );
  8. $fp = proc_open($test,$array,$pipes);   //打开一个进程通道
  9. echo stream_get_contents($pipes[1]);    //为什么是$pipes[1],因为1是输出内容
  10. proc_close($fp);
  11. ?>

6,shell_exec函数

  1. <?php
  2. $test = "ls /tmp/test";
  3. $out = shell_exec($test);
  4. echo $out;
  5. ?>

popen,passthru,proc_open,shell_exec的返回结果如下:

  1. [root@krlcgcms01 shell]# php test.php
  2. 1001.log
  3. 10.log
  4. 10.tar.gz
  5. aaa.tar.gz
  6. mytest
  7. test1101
  8. test1102
  9. weblog_2010_09

我能发现的就这几个函数,能执行linux下的命令,我想应当还有吧,欢迎大家补充。

最新文章

  1. 关于StrangeIOC框架
  2. IO流 FileOutputSteam在fos.txt写出hello
  3. Extjs 百度地图扩展
  4. [小工具]CSS内嵌样式自动提取器
  5. 廖雪峰js教程笔记8 date对象介绍和处理
  6. STL中priority_queue小结
  7. 【转】C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs封装
  8. u-boot基本命令
  9. Oracle SQL CPU占用高
  10. VS2008一个小bug
  11. js 总结累计大全
  12. Html5笔记之第六天
  13. bugku web web3
  14. 关于ipv6被拒的问题
  15. 3D Slicer中文教程(四)—图像分割
  16. JAVA递归实现全排列
  17. composer 自动加载类 通过psr
  18. Win10系列:C#应用控件基础20
  19. Java知多少(31)static关键字以及Java静态变量和静态方法
  20. 实验吧—密码学——WP之 古典密码

热门文章

  1. 实现ajax的步骤
  2. IE页面刷新ocx插件被释放,野指针非阻塞Sleep问题。
  3. Python 爬虫:把廖雪峰教程转换成 PDF 电子书
  4. nginx虚拟机配置(支持php)
  5. Disharmony Trees
  6. 记录下 Markdown 语法
  7. sass学习--在htm文件中使用
  8. Problem B: 时间和日期类(III)
  9. TOMCAT启动到一半停止如何解决
  10. 简易RPC框架-代理