zoukankan      html  css  js  c++  java
  • php system()和exec()差别

    一、exec  ---执行外部程序

    string exec ( string $command [, array &$output [, int &$return_var ]] )

    $command   要执行的shell 命令

    $output        shell命令的输出填充此数组,每行输出填充数组中的一个元素。 请注意,如果数组中已经包含了部分元素,exec() 函数会在数组末尾追加内容。如果你不想在数组末尾进行追                       加,请在传入 exec() 函数之前 对数组使用 unset() 函数进行重置。

    $return_var  命令执行后的返回状态,命令执行成功值是0

    返回值         shell命令输出的最后一行

    ps:   2>&1  exec不成功,调试方案一个技巧就是使用管道命令, 使用 2>&1, 命令就会输出shell执行时的错误到$output变量, 输出该变量即可分析。

     例子1

    (1)代码所在的index.php 文件的结构

      

    (2)代码

      

    $out = [34];
    $res = exec('ls 2>&1',$out,$return_status);
    var_dump($res);
    echo '------';
    var_dump($out);
    echo '------';
    var_dump($return_status);
    

    (3)执行结果

    zhangxueqing:demo playcrab$ php  ./1/index.php
    /Users/playcrab/www/demo/1/index.php:10:
    string(11) "webuploader"
    ------/Users/playcrab/www/demo/1/index.php:12:
    array(10) {
      [0] =>
      int(34)
      [1] =>
      string(1) "1"
      [2] =>
      string(6) "1.html"
      [3] =>
      string(5) "1.php"
      [4] =>
      string(10) "client.php"
      [5] =>
      string(14) "design-pattern"
      [6] =>
      string(3) "img"
      [7] =>
      string(17) "jquery.blockUI.js"
      [8] =>
      string(10) "static.php"
      [9] =>
      string(11) "webuploader"
    }
    ------/Users/playcrab/www/demo/1/index.php:14:
    int(0)
    

     二、system ---执行外部程序,并且显示输出

    1 string system ( string $command [, int &$return_var ] )

    $command  要执行的命令

    $return_var  命令执行后的返回状态,值是0表示成功

     1.示例代码

    $res = system('ls 2>&1',$return_status);
    var_dump($res);
    echo '------';
    var_dump($return_status);

    2.输出结果

     

     

        

  • 相关阅读:
    HDU 6040 Hints of sd0061 —— 2017 Multi-University Training 1
    HDU 6038 Function —— 2017 Multi-University Training 1
    HDU 6034 Balala Power! —— Multi-University Training 1
    使用python将excel数据导入数据库
    python reload(sys)找不到,name 'reload' is not defined
    伯乐在线资讯URL
    伯乐在线文章URL
    慕课网python分布式爬虫打造搜索引擎视频中爬取伯乐网文章
    javascript HTML DOM 简单介绍
    css样式大全
  • 原文地址:https://www.cnblogs.com/zxqblogrecord/p/9755984.html
Copyright © 2011-2022 走看看