0

我已经尝试过无数种方法来从另一个 php 脚本启动后台 php 脚本。我需要创建一个数据库文件的 zip 文件以供下载。我将其设置为立即下载单个文件,但如果需要压缩文件,我希望启动一个后台进程,该进程将通过电子邮件发送指向 zip 文件的链接。该过程有效,但原始脚本仍在等待该过程完成。我尝试了以下方法,就像我说的那样,它们有效,但它们不会进入后台。对于某些实例(大文件 zip),它让我等待了八分钟以上,没有用户会等待。

print `php -f zipfiles7.php | at now`;

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\php5.4.3\php.exe -f C:/wamp/www/zipfiles7.php", 0, false);

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /c C:\wamp\bin\php\php5.4.3\php.exe -f C:/wamp/www/zipfiles7.php", 3, false);

$exe = "C:\wamp\bin\php\php5.4.3\php.exe";
$args = "C:\wamp\www\zipfiles7.php";
pclose(popen("start \"bla\" \"" . $exe . "\" " . escapeshellarg($args), "r"));

$cmd = "php-win.exe zipfiles7.php";
$cmd = "start /B php-win.exe zipfiles7.php";
$PID = shell_exec("nohup $cmd > test.txt 2>&1");

exec("Psexec.exe -i -d  php.exe zipfiles7.php");

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
function _exec($cmd)
{
    $WshShell = new COM("WScript.Shell");
    $oExec = $WshShell->Run($cmd, 0,false);
    echo $cmd;
    return $oExec == 0 ? true : false;
}_exec("C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php");

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
pclose(popen('start /B ' . $cmd, 'r'));

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
pclose(popen("start /B " . $cmd, "r"));

system("cmd /c php zipfiles7.php > test.txt 2>&1");

exec('psexec start /B -d php.exe zipfiles7.php > test.txt 2>&1');

exec('psexec -d php.exe zipfiles7.php > test.txt 2>&1');

exec('psexec -d php.exe zipfiles7.php');

$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, "test.txt", "php_error.log"));

pclose(popen('start /B php zipfiles7.php > test.txt 2>&1', 'r'));

pclose(popen("start /B C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php", "r"));

shell_exec('start /B "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php > test.txt 2>&1"');

exec('START /B "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php"', $output);

passthru("start /B c:/wamp/bin/php/php5.4.3/php-win c:/wamp/www/zipfiles7.php > test.txt 2>&1");

exec("php.exe -f zipfiles7.php > test.txt 2>&1");

exec("php-win -f zipfiles7.php > /dell/null 2>/dev/null &", $output);

exec("start /B php zipfiles7.php > test.txt 2>&1");

exec("start php.exe -f zipfiles7.php > test.txt 2>&1");

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /C C:\wamp\www\batch.bat", 0, false);

exec("CHP.exe C:\wamp\www\batch.bat > test.txt 2>&1");

exec("start /min php zipfiles7.php > test.txt 2>&1");

shell_exec('call "cmd /c batch.bat"'); 

批处理文件

@echo off 
"start /B bg C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php > test.txt"

尝试的其他方法:

$descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("pipe", "r") // stderr is a file to write to
    );

    $cmd = "start /B php.exe zipfiles7.php";
    $process = proc_open($cmd, $descriptorspec, $pipes, null, null);
    if (is_resource($process))
    { ....

// buffer all upcoming output
ob_start();
// get the size of the output
$size = ob_get_length();

// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');

// flush all output
ob_end_flush();
ob_flush();
flush();

// close current session
if (session_id()) session_write_close();
    exec("start /B php zipfiles7.php > test.txt 2>&1");

或者

shell_exec('psexec -s -d php.exe zipfiles7.php');

或者

$runCommand = 'php -q zipfiles7.php';
    $WshShell = new COM(“WScript.Shell”);
    $oExec = $WshShell->Run($runCommand, 7, false);

甚至尝试 JavaScript

function background(){
var url = "C:\wamp\www\zipfiles7.php";// No question mark needed

xmlReq=new XMLHttpRequest();

xmlReq.open("POST",url,true);
xmlReq.send();
console.log("done");
}
echo '<script type="text/javascript">', 'background();', '</script>';

或者

function background(){
var str = "Success";
var url = "C:\wamp\www\zipfiles7.php";

xmlReq=new XMLHttpRequest();

xmlReq.open("POST",url,true);
xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlReq.setRequestHeader("Content-length", str.length);
xmlReq.setRequestHeader("Connection", "close");
xmlReq.send();
console.log("done");
}

甚至 Ajax 也不会退居幕后!

function background()
{
$.ajax(
    {
           type: "POST",
           url: "zipfiles7.php",
           data: data, // data to send to above script page if any
           cache: false,

           success: function(response)
           {
                alert('Succes');
           }
     });
}

请帮助我理解为什么该过程不会进入后台。先感谢您。

4

1 回答 1

0

当您调用 exec 时,php 会暂停当前脚本的执行,直到它从 shell(来自 exec)获得返回值。所以在这种情况下,php 不会继续执行脚本,直到文件完成压缩。

从 php exec() 文档页面:

如果使用此函数启动程序,为了使其继续在后台运行,程序的输出必须重定向到文件或另一个输出流。否则将导致 PHP 挂起,直到程序执行结束。

至于以正确的方式实现这一点,实际上我自己从来没有这样做过,所以我不能确切地说,但我希望这能为你指明正确的方向。

编辑:在进一步搜索后,我发现这个 stackoverflow 答案似乎适用于您当前的问题。

于 2013-12-06T21:16:42.460 回答