简体   繁体   English

如何使用PHP打开和关闭进程?

[英]How to open and close a process with PHP?

How to open and close a process with PHP? 如何使用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. 即使我停止了php脚本,该过程仍在后台运行(这是正常的),但是我找不到在Windows下关闭/终止它的方法。

Does anyone have an idea on how I can do that? 有谁知道我该怎么做吗? Thank you. 谢谢。

EDIT to fit mallix's answer: 编辑以适合mallix的答案:

$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. 不幸的是,7zip stanalone控制台进程仍在后台运行。 the value of the session variable is a 1 (integer) confirmed. 会话变量的值是1(整数)确认值。

Edit ..:I just noticed that the request to change the session variable with jquery is waiting.... until the first script is complete. 编辑..:我只是注意到用jquery更改会话变量的请求正在等待....直到第一个脚本完成。 So, useless :( 所以,没用:(

In your php script, check every time if a specific session is set before looping again. 在您的php脚本中,每次重新循环前都要检查是否设置了特定的会话。
When user clicks ABORT terminate that session key. 当用户单击ABORT时,终止该会话密钥。
This can also be implemented using database instead of sessions. 也可以使用数据库而不是会话来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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