I am trying to compile and run an C file stored locally in my computer using php i used the following piece of code
$dir = '/home/User/Desktop';
if ( !file_exists($dir) ) {
mkdir ($dir, 0744);
}
file_put_contents ($dir.'/test.c', $code);
$path = "/home/User/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);
i have set all the permissions to the file using chmod and chgrp but on executing it just echos "Succesful" on my screen but not my $output value This is the sample C program i typed in my
#include <stdio.h>
int main()
{
printf("Hello world");
return 0;
}
but the following program runs fine when i have executed it using GCC i am currently using Ubuntu 14.04.2 LTS
i tried using
$Command = "gcc $path 2&>1"; $output = exec($Command); $output = exec(./a.out);
but still i cant get desired output
I tried to use
$output = exec($Command,$return_val,$error);
now when i echo $error it gives me "2"
I tried using system
$last_line = system('gcc $path', $retval);
echo '
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
i get the following output
Last line of the output:
Return value: 4
sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory
but i given permission to the file using
sudo chmod g+w /home/User/Desktop/test.c
can anyone help me out with this issue?
$pathvariable, not the source code. The issue that your script outputs "Successful" is simply a result of the fact that you do not do any error checking at all. Without, how should your script know that something is wrong?/home/User/Desktopprior to running gcc.