Click to See Complete Forum and Search --> : system calls from php


TerryG
09-06-2002, 04:32 PM
Not sure if this is a unix problem or php, but I'm
posting here:


I'm trying to create a web based c++ compiler
for my computer science students. This will
allow them to submit small c++ programs and
view the errors or the output.

They type their code into a text box and when
submitted, I write there code to a file and
then attempt to compile it with a call to g++
using the system() function or ` backtick operator.

Simple unix commands work, like the following:
$command = `more hello.txt`;
echo"$command";
or
$command = `ls -l`;
echo"<pre>$command</pre>";
or
system("ls -l");

But the following will not work:

system("g++ hello.c >& err.out");
or
$command=`g++ hello.c >& err.out`;
echo"<pre>$command</pre>";

the ">&" forces the output to a file called err.out
wich gets created when ran from the command
line, but not when called from php.

I don't think it's a simple file permissions problem, played
that angle for a while.

Can this be fixed with php or is this more an administration
problem? or what?


thanks in advance

nashirak
09-07-2002, 01:41 AM
I think it actually still might be a permissions issue. I tested your program using this script:


<?php

$command = "gcc ". $com ." >& err.out";
echo $command;

system($command);

?>


then inserted a nice ?com=hello.c to try and test it. Well you are right. It will write out and create a file named err.out after gcc is done. Why? Because while it does have permission to run gcc, you do not have permission as user www-data (or nobody) to create a file inside of the directory. How did I get around this? 2 ways. First try and creating a file named err.out and changing its permissions to 755. Or if that doesnt suit you too well try creating a direcotry and changing the permissions on this directory to 755. Then stick the error file in there like:

mkdir nobody
chmod 755 nobody


<?php

$command = "gcc ". $com ." >& nobody/err.out";
echo $command;

system($command);

?>



I hope that helps.