title: Executing C code from php
body: <p>I am trying to compile and run an C file stored locally in my computer using php
  i used the following piece of code </p>
  
  <pre><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&amp;&gt;1";
  exec($Command,$return_val,$error);
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main()
  {
  printf("Hello world");
  
  return 0;
  
  }
  </code></pre>
  
  <p>but the following program runs fine when i have executed it using GCC
  i am currently using Ubuntu 14.04.2 LTS</p>
  
  <p>i tried using </p>
  
  <pre><code>$Command = "gcc $path 2&amp;&gt;1"; $output = exec($Command); $output = exec(./a.out);
  </code></pre>
  
  <p>but still i cant get desired output</p>
  
  <p>I tried to use </p>
  
  <pre><code>$output = exec($Command,$return_val,$error);
  </code></pre>
  
  <p>now when i echo $error it gives me "2" </p>
  
  <p>I tried using system </p>
  
  <pre><code>$last_line = system('gcc $path', $retval);
  echo '
  &lt;hr /&gt;Last line of the output: ' . $last_line . '
  &lt;hr /&gt;Return value: ' . $retval;
  </code></pre>
  
  <p>i get the following output </p>
  
  <pre><code>Last line of the output: 
  Return value: 4
  
  sh: 1: cannot create 1: Permission denied
  gcc: error: 2: No such file or directory
  </code></pre>
  
  <p>but i given permission to the file using </p>
  
  <pre><code> sudo chmod g+w /home/User/Desktop/test.c
  </code></pre>
  
  <p>can anyone help me out with this issue?</p>