exec("ls") works. exec("other command") does not
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 work开发者_如何转开发s 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?
Do not use relative paths. Use dirname(__FILE__)
to find the path of your PHP script, then build an absolute path from that. Example:
$lessc = dirname(__FILE__) . '/../node_modules/less/bin/lessc';
We won't give you exact answer because we don't know how your server is configured, but you must determine what is the path PHP's in and then fix you paths appropriately. The best way is to use absolute paths, as they are always resolved correctly.
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:
/usr/bin/env: node: No such file or directory
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.
精彩评论