We have built a code to compile C program from text area built in PHP. We have tried all the commands that could be used to compile the program,
for example exec(), system() and even the shell_exec() commands.
The problem is still that we are not getting the output. We are sending you the code, Kindly please suggest some solution.


echo "hi its me lalit kumar mishra";
$t1=$_GET; // $t1 is file name of user file.

$t2 .= '.o'; // we are creating the filename with .o extension

$j="cd /home/wipro;"; // where the program exist.

$s ="$j"." gcc -c $t1"; // t1 is user filename with .c extension
$ss="$j"." gcc -o $t2 $t1"; // t2 is filename with .o extension
$sss="$j"." ./$t2"; // getting output of c program

echo "Output of Program= $var1";


8 Years
Discussion Span
Last Post by nav33n

I don't know about C program, but, I wrote a simple java program and it worked. This is how I did.

if(isset($_POST['submit'])) {
	$filename = $_POST['filename']; //user will enter the filename with .java extension
	$program=stripslashes($_POST['program']); // java program
	$fp = fopen($filename,"w+"); //open the file and write the program to that file
	$path="c:\\j2sdk1.4.2_13\\bin"; // where the program exist. 
	copy($filename,$path."/".$filename); //copy to the bin directory of java for execution
	unlink($filename); //delete the file from www folder
	chdir($path); //change to directory where java is located
	exec("javac ".$filename); //compile java program
	$actualname = substr($filename, 0, strpos($filename,".")); //get the filename to execute it
	$contents = exec("java ".$actualname);  //execute java program
	echo $contents; //print the contents
<form method="post" action="executejava.php">
Filename: <input type="text" name="filename" /><br />
<textarea name="program" cols="40" rows="30"></textarea><br />
<input type="submit" name="submit" value="submit" />

Hope that helps!


Hey nav
It really works with java but I fear whether it works on linux platform or not.Our code works fine on terminal but when it gets to php linking I am doubtful about exact path values.
Will u pls tell something more about chdir() and how to cope up in linux.


Hmm.. It will surely work on Linux.. I don't have linux, so, I can't really test it. But, chdir works independent of the OS you are using.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.