3

When I run

<?php
echo exec('whoami');
?>

It works fine, even for other single word command

but for exec('gcc -o a hello.c'); it does not create object file a.

Or when I try exec('./a'); with precreated object file, it does not make any change. No error no notification!

Any help is appreciable!

7
  • 2
    Try specifying full paths to your hello.c and a. Commented Jul 1, 2013 at 9:04
  • Also depends if you have privileges to use gcc from your user is it your server or a web hosting ? Commented Jul 1, 2013 at 9:07
  • @AleksG: I tried with exec('gcc -o /opt/lampp/htdocs/a /opt/lampp/htdocs/hello.c'); but no chage! Commented Jul 1, 2013 at 9:10
  • Get information in error log file. You might have permission restrictions. Commented Jul 1, 2013 at 9:10
  • No errors? Of course: you are ignoring exec()'s optional arguments and return value, where errors would normally be reported. You're also discarding stderr. Commented Jul 1, 2013 at 9:11

3 Answers 3

3

Basic reference:

With those elements, it's straightforward to obtain error messages:

<?php

exec('gcc -o a hello.c 2>&1', $output, $return_value);

echo ($return_value == 0 ? 'OK' : 'Error: status code ' . $return_value) . PHP_EOL;
echo 'Output: ' . PHP_EOL . implode(PHP_EOL, $output);

E.g., I get this in my (Windows) computer:

Error: status code 1
Output: 
"gcc" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
Sign up to request clarification or add additional context in comments.

2 Comments

what would be the values of $output and $return_value?
Pardon? Are you asking me to guess what the output would be in your computer?
1

You could try using shell_exec() in PHP:

shell_exec('gcc -o a hello.c'); should work

Comments

1

The most likely explanation is that the user account of your web server doesn't have write access to the directory. You can determine the account name by running whoami from within php.

exec('whoami', $output);
echo $output[0];

I don't know exactly what output you're going to get from that, but let's say it's www-data which is what I see on Ubuntu.

Now, if you look at the permissions of the web root directory, you'll probably find that it's owned by another account, maybe root, and the permissions are rwxr.xr.x so only root has access.

One way to give www-data write access is to make it the owner:

chown www-data .

Alternatively you can set the group to www-data and make sure that group has write access.

chgrp www-data .
chmod 775 .

Worst case you can give everybody write access, but I wouldn't recommend this unless nothing else works.

chmod 777 .

Ideally, you should really create a separate directory with write access, and limit any output to there rather than changing the permissions on the web root directory. But you can start with the root first and make sure this really is the problem before you go to too much effort.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.