I tried to execute a command in background under Windows.
After struggling for hours with all these half ready examples I would like to share the syntax I found working (for windows at least). This is not tested under Linux as there are more elegant ways to spawn a process.
Based on the function from Arno van den Brink.
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
?>
This works perfectly with e.g.
<?php
execInBackground('del c:\tmp\*.*')
?>
but the following does NOT work:
<?php
execInBackground('\"c:\path with spaces\my program.exe\"')
?>
Why?
When windows sees quotation marks (\") it thinks this is the window title, not the command.
So, when your command needs quotation marks you HAVE TO provide a window name first, like
execInBackground("\"title\"" "\"c:\path with spaces\my program.exe\")
Quotation marks are mandatiory for window title. Otherwise windows thinks this is the program name.
Weired, but "Hey! it's Windows!" :)