PDA

View Full Version : Calling C program from PHP


stan
06-21-1999, 07:44 PM
Hi,

In one of my PHP scripts I need to call a C program to do some of the (specialized) work. In the manual I read this could be done using the popen() function. An example would be:


<?
[nbsp][nbsp]$cmd = &quot;program arguments&quot;;
[nbsp][nbsp]$fd = popen($cmd, &quot;r&quot;);
[nbsp][nbsp]if (!$fd) {
[nbsp][nbsp][nbsp][nbsp]echo &quot;Can't execute command $cmd

&quot;;
[nbsp][nbsp]}
[nbsp][nbsp]else {
[nbsp][nbsp][nbsp][nbsp]while ($buffer = fgets($fd, 4096)) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]echo $buffer.&quot;
&quot;;
[nbsp][nbsp][nbsp][nbsp]}
[nbsp][nbsp][nbsp][nbsp]$stat = pclose($fd);
[nbsp][nbsp][nbsp][nbsp]echo &quot;Stat: $stat

&quot;;
[nbsp][nbsp]}
?>


My questions:
- Is this possible in the FQ sanatized environment?
- What should the path in $cmd be for a compiled C program in my directory on SIX.

I tried to test it with $cmd = &quot;/bin/ls&quot; but got no output (and exit code 256).

- Stan

stan
06-26-1999, 07:34 AM
Nobody with experience on this subject here? :(

- Stan

Justin
06-26-1999, 09:56 AM
Unfortunately, only programs residing in PHP's SAFE_MODE exec directory can be executed from PHP :([nbsp][nbsp]I think the only thing in there now is the whois program...

If this C program outputs a content header (eg, is executable from the web) and is in your cgi-bin, you could do an fopen() on it as a remote file - but the problem is fopen() is broken right now for that use - so use file():

$myarray = file(&quot;http://www.domain.com/cgi-bin/program.cgi?args&quot;, &quot;r&quot;);

for ($i = 0; $i < count($myarray); $i++) {
[nbsp][nbsp][nbsp][nbsp]print $myarray[$i];
}

Of course the easier way would be to just use this:

print (join (file (&quot;http://blah.com/cgi-bin/program.cgi?args&quot;, &quot;r&quot;), &quot;&quot;));

If you like tight code, that is...

------------------
Justin Nelson
FutureQuest Support