简体   繁体   中英

How can I run a script in another process on Windows Server 2008, using PHP v5.4?

The main reason is because I don't want to hold up the current PHP process . I want users to be able to navigate around during the script execution.

The script in question ( importer.php ) updates a txt file with a percentage as it completes, javascript intercepts this txt file and outputs the percentage using a timer every 5 seconds to keep the user updated (all in the form of a load bar).

I've been able to launch the script like so:

$cmd = '"C:\/path\/to\/v5.4\/php" importer.php';
pclose(popen($cmd, "r"));
exit;

This runs the script, but hangs the current process until importer.php completes. Is there a way to get out of the current process and launch this using another one instead?

I read that using & at the end of the cmd tells the script to not wait, but I believe this is a *nix command and since I'm running on a Windows box, I can't use it... unless perhaps there is an alternative for Windows ?

According to the documentation at http://php.net/passthru you should be able to execute your command using that, as long as you redirect your output.

$cmd = '"C:\/path\/to\/v5.4\/php" importer.php';
// Use passthrough here, and redirect the output to a temp textfile.
passthru($cmd . '>%TEMP%\importerOutput.txt');
exit;

I was able to resolve this issue by using a WshShell Object ; WScript.Shell

$WshShell = new COM("WScript.Shell");
$WshShell->Run('"C:\/path\/to\/v5.4\/php-win.exe" -f "C:\/path\/to\/code\/snippet\/importer.php" var1 var2 var3', 0, false);

Note: I have spaces in my file structure so I needed to add quotes around the paths to the files. I was also able to pass variables, var1 , var2 , and var3 . I've also used \\/ to escape my slashes.

I'll break the Run array down a bit for my case:

  1. The first; is the command you want to run (path to php, path to script, and variables to pass).
  2. The second; 0 - Hides the window and activates another window (link below for more options).
  3. The third; false - Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true , script execution halts until the program finishes.

For more information on WScript.Shell visit http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx for details.

Hope this helps someone else!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM