user1626498
user1626498

Reputation: 139

php exec() not working with SOME of the windows environment variables

I am trying to run a couple commands from PHP with exec() and it seems to work only for some of the commands defined in my windows environment variables but not for all.
In my command prompt I can run all of this commands succesfully from any path (C:\ or E:\whatever)

> pear -V     // ... "PEAR Version: 1.9.5" ...
> dot -V      // dot - graphviz version 2.38.0 (20140413.2041)
> phpdoc -V   // phpDocumentor version 2.8.1

All of them return the correct version for each of the specified programs, that means that the environment variables are well configured.
But in PHP I can only run some commands, the rest of them fails:

$out = array();
$ret = '';

exec('pear -V', $out, $ret);
echo var_dump($out); // $ret = 0, $out = array ..."PEAR Version: 1.9.5" ...

exec('phpdoc -V', $out, $ret);
echo var_dump($out); // $ret = 1, $out = array ..."Could not open input file: \phpdoc" ...

exec('dot -V', $out, $ret);
echo var_dump($out); // $ret = 1, $out = empty array

So, am I missing something?.

EDIT: exec() is working fine, it works for some of my environment variables like pear, cmd, among others, it does not work for the specified above

phpdoc
dot (GraphViz)

Even though they work on my command prompt and are well configured in my windows environment.

EDIT2:
@Stefan Cvetkovic I gues this is the part that you want to see from the result when running this command

shell_exec("set"):

Path=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Universal Extractor;C:\Program Files (x86)\Universal Extractor\bin;C:\xampp\php; PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

Is it possible that php has no access to my "dot" path, because this is missing from that result:

...C:\xampp\php;C:\xampp\php\pear\phpDocumentor\vendor\graphviz\bin

dot.exe is located in C:\xampp\php\pear\phpDocumentor\vendor\graphviz\bin and it is well configured as I can run > dot -V from my command prompt. I have also tried changing its position in the PATH variable without luck.

Upvotes: 2

Views: 4578

Answers (4)

Tiger Fok
Tiger Fok

Reputation: 71

Although two years late, I found myself in the same situation and found a solution:

<?php
putenv('PATH=' . $_SERVER['PATH']);
$currentBranch = exec('git rev-parse --abbrev-ref HEAD');

The environment variable is missing for unknown reason in exec call,

but it does exists in $_SERVER['PATH'], the code just put it back and it works

Upvotes: 2

Marco Berger
Marco Berger

Reputation: 51

I was just having the same issue. In case anyone is having the same problem, make sure to restart the webserver (apache in my setup) because the PATH-variable was set recently and therefore was not available for the process (httpd) which was started prior to setting the variable.

Upvotes: 1

Stefan Cvetkovic
Stefan Cvetkovic

Reputation: 144

Try using shell_exec().

On linux platform for example i can't use exec to retrieve lm-sensors data, but with shell_exec i can, and you will get complete output as sting.

shell_exec() manual

Edit:

Try this:

$command = new COM("WScript.Shell");
$shellCommand = $command->Exec("C:\program.exe");

Now grab the output.

$standard = $shellCommand->StdOut->ReadAll;    # Standard output
$error = $shellCommand->StdErr->ReadAll;       # Error

Upvotes: 0

Adam T
Adam T

Reputation: 675

I would try using gettype on arrays or vars that are in question.

Barring that, I would also run the phpinfo() to see if that item is enabled in your server (or php.ini which I'm sure you checked?).

Saw a few interesting notes on exec exec() and shell exec http://php.net/manual/en/function.shell-exec.php, where an item needs admin privileges to run. Perhaps it applies?

Upvotes: 0

Related Questions