If I put this in my script:
$output = array();
exec("/var/www", $output);
var_dump($output);
I get an array of all the files in that folder.
However when I do this:
$output = array();
exec("../node_modules/less/bin/lessc ./default.less", $output);
var_dump($output);
The output I get is
array(0) {}
If I run the same command in the command line (from the same directory my php script is in) it works fine.
I also tried absolute paths:
/var/www/node_modules/less/bin/lessc /var/www/nodeless/default.less
Here is the permissions for these files:
drwxr-xr-x 2 ftpuser ftpuser 4096 2011-06-06 16:43 nodeless
drwxr-xr-x 4 ftpuser www-data 4096 2011-06-06 15:44 node_modules
Any ideas why I can’t do it from PHP?
I really owe finding the solution to my problem to the help from Radu and Tomalak Geret’kal. However as Radu’s answer currently stands, it does not describe what fixed my issue. So I will explain it here.
I tried to SSH into the server using the same user as PHP (www-data) and execute the same command. I was given this error:
As it turns out when I first installed
node.js, the instructions I followed told me to add the path to my $PATH environment variable. However that was a different user than www-data, so the path to node was not in my $PATH any longer when using the www-data user.I am not expert in this area but I assumed that was likely the problem. So I started over following the installation instructions for node.js, only this time as the www-data user.
Afterwards I could then execute the command on the command line as www-data. So I tried my PHP script again. It now worked as well.