would output the servers last reboot time, by executing the command in ()'s.
(was looking at my admin's .profile for how he got unix to spit out all the Hi blah blah, its blah blah time stuff when I logged in)

I thought this was super slick since I had been trying to do this for awhile in CGI/Perl.

Since the thing I want to do is a compiled program (using gcc to compile a .c file) would I have to pipe its output to a file and then use the shell_exec to print the contents of the file?

Also, is the php manual good for learning from, or a good resource?

The one book I have that mentions php only has one chapter, and Im too poor to buy any more books this semester :p

I guess I could go through my directories and remake everything in php for starters.

firepages

10-09-2004, 03:24 AM

your executable should just print to stdout , no need to save to file unless you have that specific requirement.

exec() basically returns stdout from whatever it is executing, if the output is multiline then PHP loads it as an array..

<?
exec('du -h' , $yaks);
print_r($yaks);
?>

executables that require further input from the user (in this case PHP) are another matter , you can use proc_open (http://www.php.net/proc_open) to `talk` to an executable but its easier said than done ;)

if an exec hangs or returns nothing (when you expected it to) redirect stderr to stdout
<?exec('some_command -switch args &2>1',$yaks);?>

nero0102

10-09-2004, 05:32 AM

First, thanks for the quick reply.

I tried your code substituting my program name and variable name.

and my HTML output was

Array()

I looked at the manual page for print_r, and im confused as you how that would handle say a paragraph of text.

would it be an array of strings? where each index would be a line ending in a \n ?

so the array would be

1 => Hello\n
2 => The sky is blue\n
3 => and cows go moo\n

Would I get the blank array output because this program is not from the admin ?

The program is just on my directory (same as my site, and is chmod to 755).

t

firepages

10-09-2004, 06:26 AM

chmod to 0755 should be enough for PHP to exec the file.

As far as I know PHP exec() simply turns output into an array based on newlines & print_r() is just an quick way to view that data.

did you try adding &2>1 to the end of the command string ?
<?$command = "/path/to/binary $args &2>1";?>