0

I'm trying to execute a .exe file form PHP.

I've set the PATH variable and confirmed that it works by running my command from random directory.

PHP code:

$command = "myCommand >> log.txt 2>&1";
echo exec($command, $output, $return)."<br>";

I've inspected the log.txt and it contains an error:

'myCommand' is not recognized as an internal or external command,
operable program or batch file.

What could be the reason for this issue? it seems that PHP won't look into all directories specified inside of PATH. What do you recommend that I do?

Specifying full path to myCommand works, but I'd like to omit it. I am running Windows 10.

sanjihan
  • 5,592
  • 11
  • 54
  • 119
  • Which OS are you using? There is at least https://stackoverflow.com/questions/3428647/php-exec-path-variable-missing-elements which explains this behavior. – Progman Jul 29 '18 at 20:48
  • I am using windows. – sanjihan Jul 29 '18 at 20:51
  • 1
    What is the output of `var_dump(getenv("PATH"));` and how does it relate to your configured PATH variable of your OS? – Progman Jul 29 '18 at 20:51
  • Hm. It is missing the folder in which myCommand.exe is stored. This further proves that something funky is going on with php. – sanjihan Jul 29 '18 at 20:54
  • How do you run the PHP script? Do you run it manually in a Command Prompt or is it executed automatically by the web server? – axiac Jul 29 '18 at 21:00
  • When have you added the directory of your "myCommand.exe" file to your PATH variable? Have you restarted the web server? Have you restarted the shell which runs the PHP script? Have you restarted the OS? – Progman Jul 29 '18 at 21:00
  • @Progman restarting the OS is not needed and the first question is not "when" but "how". – axiac Jul 29 '18 at 21:01
  • 1
    PHP actually doesn't care too much about your `PATH`. As the [documentation of `exec()`](http://php.net/manual/en/function.exec.php#refsect1-function.exec-notes) explains, `exec()` starts the Command Prompt (`cmd.exe`) and passes to it the command line. It's the job of `cmd.exe` to find `myCommand` in `%PATH%` and start it. This is also visible from the error message (*"'myCommand' is not recognized as an internal or external command, operable program or batch file."*) that belongs to `cmd.exe`. – axiac Jul 29 '18 at 22:09

0 Answers0