note 119331 added to function.exec

From: Date: Sat, 14 May 2016 13:33:20 +0000
Subject: note 119331 added to function.exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-205484@lists.php.net to get a copy of this message
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!" :) ---- Server IP: 213.172.110.13 Probable Submitter: 87.133.15.78 ---- Manual Page -- http://php.net/manual/en/function.exec.php Edit -- https://master.php.net/note/edit/119331 Del: integrated -- https://master.php.net/note/delete/119331/integrated Del: useless -- https://master.php.net/note/delete/119331/useless Del: bad code -- https://master.php.net/note/delete/119331/bad+code Del: spam -- https://master.php.net/note/delete/119331/spam Del: non-english -- https://master.php.net/note/delete/119331/non-english Del: in docs -- https://master.php.net/note/delete/119331/in+docs Del: other reasons-- https://master.php.net/note/delete/119331 Reject -- https://master.php.net/note/reject/119331 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205484) next »