Using exec on php/Windows

I tried to run a program with exec() but it just didn’t run. It was a program with several parameters, each parameter escaped with escapeshellarg().

It didn’t do anything, had no return value and no output. After putting a pair of extra quotes around the whole command it worked:

  1. $arg1 = escapeshellarg("first argument");
  2. $arg2 = escapeshellarg("second argument");
  3. $cmd = "C:\\do-something.bat $arg1 $arg2";
  4. exec("\"$cmd\"");

In the German php documentation I found the hint that exec() executes cmd /c with exec’s first parameter as argument. Therefore the whole command has to be quoted.

Tags: , ,

Leave a Reply

Page optimized by WP Minify WordPress Plugin