How do you call a c program from php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xpr0metheusx
Forum Newbie
Posts: 1
Joined: Tue Aug 06, 2002 11:43 am
Contact:

How do you call a c program from php?

Post by xpr0metheusx »

Ok, my problem is this - I need to call a c program from a php. I also need to pass a string to

it and have it return an array or string. I was thinking about calling it like its cgi script,

but I'm not to sure how to do that. I haven't figured the variable passing because I'm just

trying to get it to run first.
I tried -
passthru("./webetest 2>&1");
and the output is-
sh: ./webetest: Permission denied
I also tried -
$output3= `./webetest`;
echo "output3 :<pre>$output3</pre><br>";
The output is blank
Next I tried-
passthru("g++ webetest.c 2>&1");
And it produces -

In file included from webetest.c:28: FDXParser.h:43:23: warning: no newline at end of file

FDXParser.h:43:23: warning: no newline at end of file /usr/bin/ld: cannot open output file a.out:

Permission denied collect2: ld returned 1 exit status

I tried shell_exec(), system(), and exec() functions. None work. Does anyone know ho to do this?

Or does anyone have example code.

Thank you,
KW
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Another Approach

Post by musashi »

Running a C program should work with passthru... It seems that your problem might lie in the permissions you have given your program (you most likely are going to need open your program to world execution in order to get it to run). You might also consider doing a bash script or something as a "middle-man" to start your app. This could also eliminate the problem of PHP hanging if your C program gets into an infinite loop.

Bash scripts, or any shell script, is simple enough. Try this:

Code: Select all

#!/bin/bash

./webetest
If you need to pass data to it, you could do so by creating a file to pass in, and then reading a file that webtest writes to (using the < and > symbols).

Granted this is a shoestring method :roll:, but it should work. I would suggest you try the permission settings first.
Post Reply