I have a feeling it is to do with permissions, I compiled the following program with g++
main.cpp
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "It works!" << std::endl;
return 0;
}
and I have the PHP file
test.php
<?php
// DEBUG /////////////////////////////////
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
//////////////////////////////////////////
$in = "/home/alex/Dropbox/code_snippets/output/a.out";
$out = exec($in);
echo "In : " . $in . "<br>";
echo "Out: " . $out . "<br>";
?>
Running the program on the command line I get the output "It works!" however when I run on the PHP I don't get anything for the output (I still get the [In:...\nOut:] though, I know PHP is working).
Related question: php exec() is not executing the command
Made me put the " 2>&1" at the end and now I get an output of:
Out: sh: 1: /home/alex/Dropbox/code_snippets/output/a.out: Permission denied
After doing a quick sudo chmod 0777 to the a.out I still get the same thing. Also tried sudo chown www-data a.out to give ownership to the PHP but that didn't work either, I'm sure it's pretty simple but I am finding difficulty getting the solution. Thanks in advance.
edit:
exec() function definitely works because I used exec("whoami") to find out that it is the user www-data that is executing it through the PHP.
tried opendir() function on the directory and it returned the same permission denied
Warning: opendir(/home/alex/Dropbox/code_snippts/output): failed to open dir: Permission denied in /var/www/html/test.php
I'm guessing it is because the PHP is limited to only executing stuff in /var/www after having a bit more of a look on the internet I have found this might help:
https://help.ubuntu.com/community/ApacheMySQLPHP and scroll down to the part Installing suPHP that seems to be a way of setting up the ability to run stuff in a directory other than /var/www has anybody else got any better ideas?
Copied a.out to /var/www and the PHP is outputting "It works!".