zoukankan      html  css  js  c++  java
  • php执行linux命令的6个函数

    一般情况下,很少会用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 ";  
    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.   
    5. while (!feof($fp)) {      //从通道里面取得东西  
    6.  $out = fgets($fp, 4096);  
    7.  echo  $out;         //打印出来  
    8. }  
    9. pclose($fp);  
    10. ?>  

    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.   
    9. $fp = proc_open($test,$array,$pipes);   //打开一个进程通道  
    10. echo stream_get_contents($pipes[1]);    //为什么是$pipes[1],因为1是输出内容  
    11. proc_close($fp);  
    12. ?>  

    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  
  • 相关阅读:
    verilog BRAM 读写
    verilog 语法一 led 翻转
    面试 遇到 问题
    S32K144+UJA1169 (四 ) S32K144 SPI1 功能初始化
    S32K144+UJA1169 ( 三 ) S32K144 SPI1 功能初始化
    S32K144+UJA1169 ( 二 ) S32K144 SPI1 对应的引脚 初始化 为 SPI 功能
    S32K144+UJA1169 ( 一 ) 连接框架+1169 功能 说明
    编译 xboot
    make clean make[1]:sdl2-config:命令未找到
    lwip 内存配置和使用,以及 如何 计算 lwip 使用了多少内存?
  • 原文地址:https://www.cnblogs.com/likui360/p/7212908.html
Copyright © 2011-2022 走看看