Search code examples
phpcshell-exec

Running a C compiled program through a PHP webpage under linux


My page.php file looks like this at the moment:

    <?php

    $cmd = './foo < input.txt > output.txt';

    $result = shell_exec($cmd);

    echo system('date'); //just to see change when refreshing the page

    ?>

Whenever I type the same command in the shell, it works perfectly (with the input.txt file ready with an example input).

My idea was to create an interface using php to communicate with the input.txt file and then communicate with the output.txt file to return the results on the page. But, first of all, I need to make sure I can run the program I have.

I've compiled with

    gcc -Wall -o foo foo.c

The foo executable is on the same directory as the page.php.

I've tried with 777 permissions on all the files (page.php, input.txt and foo)

The site is up and running and the date changes when I refresh the page but there's no output.txt on the directory.

I've tried

    $cmd = 'ls -la';

    $result = shell_exec($cmd);

    echo $result;

and it works as intended, showing the contents of the proper directory.


Solution

  • As stated in a comment, the problem was permission based. The minimum permissions required for this to work are as follows

    "711 on the executable files, 644 on input file, 666 on output file and 755 on containing folder."

    Thanks for all the contributions.