• php 执行外部命令exec() system() passthru()


    php 执行部命令exec() system() passthru()

    通常用c写一个外部小程序,然后使用上述命令可以在php中调用

    1. exec()

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

    $command要执行的外部程序

    $output 会把程序中所有的输出结果输出到该数组中;如c中的printf(); 可以利用这个往外部返多个值;

    $return_var 该程序执行结果的返回值;比对 C程序中 的return 0; 

    string 返回值即程序输出的最后一个行,即最后一个printf()

    如下示例:

    test.c

    #include <stdio.h>
    
    int main(int argc, const char * argv[]) {
        if (argc==2) {
            printf("this is parm %s
    ",argv[1]);
        }
        printf("Hello, World!
    ");
        return 0;
    }

    编译test.c到可执行文件

    cocoaPro:Desktop cocoajin$ gcc -o test main.c 
    cocoaPro:Desktop cocoajin$ ./test ppp
    this is parm ppp
    Hello, World!
    cocoaPro:Desktop cocoajin$ ls |grep test
    test
    cocoaPro:Desktop cocoajin$ 

    test.php

    <?php 
            
            echo "hello world from php <br>";
    exec("./test 'aaa'", $outArry,$dret);
    
            echo $dret.'<br>';
    
            echo var_dump($outArry);
        
     ?>

    访问test.php输出

    hello world from php 
    0
    array(2) { [0]=> string(16) "this is parm aaa" [1]=> string(13) "Hello, World!" }

    注意上面的test 程序,如果php的环境是linux的,就要用linux下的gcc编译,winddows环境,就要用win下的gcc编译;

      在linux下编译的test程序,在win下是不能用的

    2.system()

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

    $command 要执行的命令

    $return_var 程序的返回值;即C程序中的return值0;

    string 函数返回值是,程序执行的最后一行输出;

    3. passthru()

    void passthru ( string $command [, int &$return_var ] )

    $command 要执行的命令

    $return_var 程序的返回值;即C程序中的return值0;

    无返回值;

    总结:

    这几个命令功能真强大,php结合C,可以做很多事情了!

    参考:http://php.net/manual/zh/function.exec.php

     

  • 相关阅读:
    容斥原理学习(Hdu 4135,Hdu 1796)
    ACdream 1112
    CodeChef--Cards, bags and coins
    ACdream 1108(莫队)
    Hdu 2586(LCA)
    CodeChef--EQUAKE
    Hackerrank--Mixing proteins(Math)
    Clash Credenz 2014 Wild Card Round题解
    Codeforces 463D
    CodeChef August Lunchtime 2014 题解
  • 原文地址:https://www.cnblogs.com/cocoajin/p/5805679.html
Copyright © 2020-2023  润新知