Passing data between PHP and C executable in linux

前端 未结 4 516
迷失自我
迷失自我 2021-01-12 15:12

Under Linux ,if I want to pass pure string from PHP to C, how do i do that? what I\'ve tried do far is:

exec(\"./myexec.bin -a mystring\");

4条回答
  •  不知归路
    2021-01-12 15:53

    You could use popen() to open a pipe to the executable:

    $fp = popen('./myexec.bin', 'w');
    fwrite($fp, $data);
    pclose($fp);
    

    Then, as previously suggested, read from stdin in your C program:

    fopen(stdin, "r");
    // ...
    

    It is "safer" to use popen() rather than exec('/bin/echo') because you can write characters that would otherwise be interpreted by the shell (&, |, ...). Note that the handle returned from PHP's popen() must be closed with pclose().

提交回复
热议问题