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.

<?php

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

$t2=$t1;
$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
exec($s);
exec($ss);
$var1=exec($sss);

echo "Output of Program= $var1";

?>

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

<?php
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
	fwrite($fp,$program);
	fclose($fp);
	$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
}
?>
<html>
<body>
<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" />
</form>
</body>
</html>

Hope that helps!
Cheers,
Naveen

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.