i created a file by the following code
$dir = '/home/srikanth/Desktop';
if ( !file_exists($dir) ) {
mkdir ($dir, 0777);
}
//$code contains the code C code which i want to execute
file_put_contents ($dir.'/test.c', $code);
$path = "/home/srikanth/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);
when i open my error log file i see the following error
sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory
i tried the following commands to change the permission ,I am currently using Ubuntu 14.04.2 and Apache server
chmod 0777 /home/srikanth/Desktop/test.c
sudo chmod -R 777 /home/srikanth/Desktop/test.c
sudo usermod -aG www-data root
addgroup www-data
I used these commands to add www-data to root group but i still keep on getting the same file in my error log
As mentioned in the comments, this issue is a permissions issue, in so much as the server user can not create folders/files in the /home/srinkanth/Desktop directory. Or something in the gcc compiler can not create files during compilation of your c code.
Adding 777 to that DIRECTORY (not the test file on its own) will likely fix the problem. However, good practice is to add exits/exceptions in your code to give you more visibility on problems if and when they happen and what caused them.
Here is my advice:
$dir = '/home/srikanth/Desktop';
if (!is_dir($dir)) {
$mkDir = mkdir($dir, 0777);
if (!$mkDir) {
exit('Failed to create ' . $dir);
}
}
Then you can add additional de-bugging info to your put file code:
$codePath = $dir.'/test.c';
$created = file_put_contents($codePath, $code);
if (!$created) {
exit('Failed to create ' . $codePath);
}
The if you reach this stage more to your execute file code:
$codePath = $dir . '/test.c';
$command = "gcc $codePath 2&>1";
$output = [];
$returnStatus = '';
$lastLine = exec($command, $output, $returnStatus);
Then you can output your whole gcc compile by glueing the output array pieces back together with a new line as exec parses each line of output as a record in the output array:
echo implode("
", $output);
Hope this helps. Goodluck.