I'm trying to launch a really time-consuming script from a browser such that the user can start the script and then continue to browse other material while the long script runs in the background. I've got this working quite well for linux here, and I've been trying this solution from Drakla for windows and it's just not working for me.
My script, launch.php, looks as follows:
<?php
$cmd = 'php long_script.php'; // just a script that sleeps a lot and updates a mysql table with its progress
echo 'cmd:' . $cmd . '<br>';
$result = run_in_bg($cmd);
print_r($result);
function run_in_bg($cmd, $winStyle = 0, $waitOnReturn = false){
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($cmd, $winStyle, $waitOnReturn);
# this line causes error
# $WshShell->Release();
return $oExec;
}
?>
The output of the script looks like this:
cmd:php long_script.php
0
both launch.php and long_script.php live in the same directory. I access launch.php via a browser but long_script.php never reports any progress. I turned on error_logging in my PHP.ini on this windows machine and discovered that long_script.php was running but PHP was dying of a fatal error when trying to include these scripts inside long_script.php:
include_once "../framework/scripts/framework_cli.php";
include_once "util.php";
include_once "constants.php";
These includes work and make complete sense when evaluated relative to the file long_script.php (or relative to launch.php) but do not work when one assumes the current working dir is C:\Program Files\Apache Software Foundation\Apache2.2. For some reason, this is what PHP on windows assumes when I invoke a script in this manner.
Can someone explain this to me? The windows behavior is totally different than the linux behavior and i bet it has something to do with the gnarly PATH var I have on my windows server -- or perhaps something to do with 'WScript.Shell'.
I'm both looking for some reason to this madness but also a good way to deal with it. The idea of putting a chdir() statement in every script I'd like to invoke with this method sounds like a pretty bad way to do it. NogDog suggested this but I'm still thinking it's pretty ugly:
chdir(dirname(__FILE__));
EDIT: I made various changes for clarity.