Calling gcc from within PHP code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joxtoby
    New Member
    • Dec 2006
    • 4

    #1

    Calling gcc from within PHP code

    I am currently trying to create a php script that will call gcc and pass various arguments so that you can compile a c program on a remote server after uploading your raw code. I'm using something along the following lines but as of yet it's not working:

    [PHP]
    $command = "gcc /home/username/sourcefile.c";
    $output = shell_exec($com mand);
    echo $output;
    [/PHP]

    When I run this I get a blank browser window, and the executable file that should be produced is not. I have tried creating a batch file containing the $command string and then replacing $command in my script with
    [PHP]
    $command = "sh batchfile";
    [/PHP]
    but I get the same result.

    Any ideas? I know the shell_exec command works (i.e. PHP safe mode is not on) as I can run an "ls" command just fine, it appears only the call to gcc does not work, even if I put in the full path (/usr/bin/gcc). The script is running on a web hosting site Linux server and I've verified that gcc exists. I have also tried running this on a different Linux server at work with the same results.

    Many thanks for any help!
    Jon
  • b1randon
    Recognized Expert New Member
    • Dec 2006
    • 171

    #2
    Originally posted by joxtoby
    I am currently trying to create a php script that will call gcc and pass various arguments so that you can compile a c program on a remote server after uploading your raw code. I'm using something along the following lines but as of yet it's not working:

    [PHP]
    $command = "gcc /home/username/sourcefile.c";
    $output = shell_exec($com mand);
    echo $output;
    [/PHP]

    When I run this I get a blank browser window, and the executable file that should be produced is not. I have tried creating a batch file containing the $command string and then replacing $command in my script with
    [PHP]
    $command = "sh batchfile";
    [/PHP]
    but I get the same result.

    Any ideas? I know the shell_exec command works (i.e. PHP safe mode is not on) as I can run an "ls" command just fine, it appears only the call to gcc does not work, even if I put in the full path (/usr/bin/gcc). The script is running on a web hosting site Linux server and I've verified that gcc exists. I have also tried running this on a different Linux server at work with the same results.

    Many thanks for any help!
    Jon
    This might be a long shot but have you checked for a permissions problem? I think php usually runs things in a really underprivileged account. See if you can change the permissions or verify that they are adequate. Good luck! This is a neat task.

    Comment

    • joxtoby
      New Member
      • Dec 2006
      • 4

      #3
      Originally posted by b1randon
      This might be a long shot but have you checked for a permissions problem? I think php usually runs things in a really underprivileged account. See if you can change the permissions or verify that they are adequate. Good luck! This is a neat task.
      That was the problem! Can't believe I overlooked it, many thanks!

      Comment

      • macklin01
        New Member
        • Aug 2005
        • 145

        #4
        I've noticed similar permissions issues when you want PHP to write a log, for instance.

        I have a question, though: what's the best way to elevate the permissions so that PHP can do what it needs without overly compromising security? (Assuming a *nix environment.) Certainly, doing a chmod a+w isn't the greatest way to go. ;)

        Thanks for posting on a fun topic. :) -- Paul

        Comment

        Working...