How to open and close a process with PHP?
Right now I open a process with the following code:
$zoutput = array();
if( ($fp = popen("7za t \"".$path."\" * -r", "r")) ) {
while( !feof($fp) ){
$fread = fread($fp, 1024);
$line_array = preg_split('/\n/',$fread);
foreach($line_array as $a) {
if($a != "") {
$zoutput[] = $a;
}
}
//$_SESSION['job'][$jobid]['currentfile'] = $_SESSION['job'][$jobid]['currentfile']+1;
flush();
}
pclose($fp);
}
I'm looking into a solution to close the process in case the user want to abort. Even if I stop the php script, the process still runs in the background (and that's normal) but I can't find a way to close/terminate it under Windows.
Does anyone have an idea on how I can do that? Thank you.
EDIT to fit mallix's answer:
$zoutput = array();
if( ($fp = popen("7za a -t7z ".$GLOBALS["backup_compression"]." \"".$_SESSION['job'][$jobid]['bFQN']."\" \"".$pathtobackup."\"", "r")) ) {
while( !feof($fp) ){
//Verifying if the user want to abort the process
if(isset($_SESSION['job'][$jobid]['AbortCurrentJob']) && ($_SESSION['job'][$jobid]['AbortCurrentJob'] == 1)) {
pclose($fp);
return;
}
$fread = fread($fp, 1024);
$line_array = preg_split('/\n/',$fread);
foreach($line_array as $a) {
if($a != "") {
$zoutput[] = $a;
}
}
//$_SESSION['job'][$jobid]['currentfile'] = $_SESSION['job'][$jobid]['currentfile']+1;
flush();
}
pclose($fp);
}
echo json_encode($zoutput);
Unfortunatly the 7zip stanalone console process still run in the background. the value of the session variable is a 1 (integer) confirmed.
Edit ..:I just noticed that the request to change the session variable with jquery is waiting.... until the first script is complete. So, useless :(