pokiri
pokiri

Reputation: 87

Executing C code from php

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?

Upvotes: 2

Views: 122

Answers (1)

aib
aib

Reputation: 46921

It's not 2&>1, it's 2>&1.

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

The first error you see here is the shell complaining about not being able to create a file named "1" to redirect stdout.
The second error is gcc complaining about the lack of an input file called "2".

Upvotes: 1

Related Questions