0

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:

  1. 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.

  2. 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!".

user3102241
  • 487
  • 1
  • 7
  • 18
  • Have you checked your php.ini to make sure you're allowed to use the exec() function? Specifically check if it's listed in `disable_functions` – mz3 Sep 04 '14 at 15:28
  • Where is your PHP file located? Can you open directory /home/alex/Dropbox/code_snippets/output/ from it? (by opendir(), function for example) – Petr Sep 04 '14 at 15:32

3 Answers3

0

Your code works fine for me, so it'll have to be an environment configuration problem.

1) exec() is blacklisted in php.ini

Check php.ini to see if safe_mode is on, or if exec() is disallowed in disable_functions.

2) The directory containing your file is disallowed by the webserver configuration.

You haven't said if you're using a webserver, but make sure there's a directory block like the following in your server config if that's the case (example for apache):

<Directory /var/www/>
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>
mz3
  • 1,314
  • 11
  • 27
  • exec is definitely working as I tried: `exec("whoami")` and it returned www-data – user3102241 Sep 10 '14 at 17:38
  • Emzy, do you know where the apache configuration file is located that contains the information I need to change as shown above? – user3102241 Sep 10 '14 at 17:48
  • By default it's in /etc/apache2/apache2.conf (Debian-based) but could be different depending on your OS. – mz3 Sep 10 '14 at 17:49
0

You may need to use a umask :

$umask = umask(0);
$in  = "/home/alex/Dropbox/code_snippets/output/a.out";
$out = exec($in);
umask($oldumask);
matwr
  • 1,548
  • 1
  • 13
  • 23
0

It's a problem with permissions the user: www-data cannot read/write/execute anything outside /var/www directory (by default). Thanks to emzy and Petr who helped me work this out.

user3102241
  • 487
  • 1
  • 7
  • 18