2

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.

2
  • Shouldn't the exec() be disabled by safe mode? Have you set the safe mode off? Commented Mar 2, 2015 at 15:39
  • exec() is fine, it works with some commands, but it is not working with some others. Commented Mar 2, 2015 at 15:49

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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?

Comments

0

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

3 Comments

I've tried shell_exec() as well, with the same results: echo shell_exec('phpdoc -V'); // Could not open input file: \phpdoc. Also, for echo shell_exec('dot -V'); // empty string
Can you try first to run shell_exec("set"); and post result.
I added a second edit (EDIT2), my path variable is OK, but it seems that php can't access to all of its elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.