English 中文(简体)
在指挥工作仍在进行时,分级从A.S.A.A.C.中得出结果?
原标题:Grab results from a php exec() while the command is still running?
  • 时间:2011-10-09 11:51:07
  •  标签:
  • php
  • exec

当我从PHP中像样操作exec时:

$result = exec( command );

评估结果将储存在<代码>下。 但就我目前的情况而言,我的指挥可以花几分钟时间,产出随着时间的推移而产生。 在运行期间,我能否获得产出? 我知道<代码> 绕航。 这种方法将产生结果,成为浏览器,但我实际上希望直接获得。

最佳回答

http://php.net/proc_open"rel=“nofollow noreferer”

在使产出流不锁定(stream_set_blocking<>/code>)之后,只要你想要,就可读到该文本,而不必阻挡你的购买力平价编码。

-Edit- If you use

$result = exec( command > /path/to/file & );

It will run in the background and you can read the output in /path/to/file

问题回答

也许不是这样做的最佳方式(但为我工作):

<?php

$cmd = "ping 127.0.0.1 -c 5"; //example command

$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("pipe", "a")
);

$pipes = array();

$process = proc_open($cmd, $descriptorspec, $pipes, null, null);

echo "Start process:
";

$str = "";

if(is_resource($process)) {
    do {
        $curStr = fgets($pipes[1]);  //will wait for a end of line
        echo $curStr;
        $str .= $curStr;

        $arr = proc_get_status($process);

    }while($arr[ running ]);
}else{
    echo "Unable to start process
";
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

echo "


Done
";

echo "Result is:
----
".$str."
----
";

?>

第2条

exec( command , $result);

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as , is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

或许可以实现你需要使用passthru(output缓冲ing 。 但并不确定。

为我的目的,我利用Edddie的回答并修改了答案(向MySQL的垃圾场投放,不淹没服务器的RAM)。

$dumpCommand = "mysqldump --skip-lock-tables -u $dbuser -p$dbpasswd $dbname";
$dumpFileName =  backup_ .$dbname. - .date( Ymd-Hi ). .sql ;

$descriptorSpec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("pipe", "a")
);

$pipes = array();

$process = proc_open($dumpCommand, $descriptorSpec, $pipes, null, null);

if(!is_resource($process)) {
    die( Unable to start process );
}

header( Content-Type: application/octet-stream );
header( Content-Disposition: attachment; filename=" .$dumpFileName. " );

do {
    echo fgets($pipes[1]); // Will wait for EOL
    $arrStatus = proc_get_status($process);
} while($arrStatus[ running ]);

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...