在linux PHP和C可执行之间传递数据 [英] Passing data between PHP and C executable in linux
问题描述
在Linux下,如果我想从PHP传递纯粹的字符串,C,我该怎么办呢?
我已经试过做多远:
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");
在PHP以及
getopt(argc,argv, "a:");
用C
一切正常,但是当我传递一个字符串久一些比 MAX_ARG_STRLEN
(131072),它将不再返回0,而不是返回127找不到命令....
everything works, but when i pass strings longers than MAX_ARG_STRLEN
(131072), it will no longer return 0 instead it returns 127 which is command not found....
有没有其他的方法来字符串数据传递到Linux的可执行文件?或者是有什么办法来克服 MAX_ARG_STRLEN
问题?
is there any other ways to pass string data to a linux executable? or is there any way to overcome the MAX_ARG_STRLEN
problem?
推荐答案
您可以使用 的popen()
打开一个管道可执行文件:
You could use popen()
to open a pipe to the executable:
$fp = popen('./myexec.bin', 'w');
fwrite($fp, $data);
pclose($fp);
然后,previously建议,从标准输入
在你的C程序如下:
fopen(stdin, "r");
// ...
这是更安全使用的popen()
,而不是 EXEC('/斌/回声')
因为你可以写个字符,否则将被shell PTED间$ p $(安培;,|,...)。请注意,把手从返回PHP的的popen()
必须与关闭pclose函数()
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()
.
这篇关于在linux PHP和C可执行之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!