PHP在浏览器超时和echo输出中执行同一行

时间:2016-01-05 13:10:15

标签: php

我有一个向客户发送电子邮件的脚本

<?  
    set_time_limit(0);
    error_reporting(E_ALL);

    $_SESSION['start_time'] = time();
            $strSubject = strip_tags($_POST["txtSubject"]);
            $strSubject = utf8_encode($strSubject);
            $strMessage = $_POST["txtDescription"];
            $txtFormName = $_POST["txtFormName"];
            $txtFormName = utf8_encode($txtFormName);
            $txtFormEmail = $_POST["txtFormEmail"];
            $mailSended = 0;
                $count = 0;
                $Ccount = 0;
                foreach($_FILES['fileAttach']['error'] as $status){
                    if($status === UPLOAD_ERR_OK) {
                    $fname[] = $_FILES['fileAttach']['name'][$Ccount];
                    $tmp_path[] = $_FILES['fileAttach']['tmp_name'][$Ccount];
                    $ftype[] =  $_FILES['fileAttach']['type'][$Ccount];
                        $count++;
                    }
                    $Ccount++;
                }
            //if not empty post txtTo
        if(!empty($_POST['txtTo'])){
            $recipients = str_replace(';',',', $_POST['txtTo']);
            $recipients = explode(',', $recipients);

            foreach ($recipients as $ReCEmail)  {
            ob_start();   
                SendEmails($ReCEmail,$strSubject,$strMessage,$txtFormName,$txtFormEmail,$fname,$ftype,$tmp_path);
            ob_end_flush();
            } 
        }    //if empty post txtTo  takes emails from file
        else 
        {
            $handle = fopen(getcwd() . "/emails.txt", "r");
            if ($handle) {
                while (($line = fgets($handle)) !== false) {
                ob_start();
                    SendEmails($line,$strSubject,$strMessage,$txtFormName,$txtFormEmail,$fname,$ftype,$tmp_path);
                ob_end_flush(); 
                }
                fclose($handle);
            } else {
                echo "error opening the file.";
            } 
        }

        function SendEmails($vasia,$strSubject,$strMessage,$txtFormName,$txtFormEmail,$fname,$ftypes,$tmp_path) {

            $strTo = $vasia; 
            $txtFormName = utf8_decode($txtFormName);
            $strSid = md5(uniqid(time(),true)).base_convert(time() * 2, 10, 36);  
            $strHeader = "X-Priority: 0 (Highest)" .PHP_EOL; 
            $strHeader .= "Importance: High" .PHP_EOL;
            $strHeader .= "From: ".$txtFormName." <".strip_tags($txtFormEmail).">" .PHP_EOL ."Reply-To: ".strip_tags($txtFormEmail);
            $strHeader .= "MIME-Version: 1.00" .PHP_EOL;
            if(count($fname) == 0) 
            {
                $strHeader .= "Content-type:text; charset=\"UTF-8\"" .PHP_EOL;
                $strHeader .= "Content-Transfer-Encoding: 7bit"  .PHP_EOL;
                $strHeader .= $strMessage .PHP_EOL;
            } else {
                $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"".PHP_EOL;
                $strMSG = "This is a multi-part message in MIME format.".PHP_EOL .PHP_EOL;
                $strMSG .= "---".$strSid.PHP_EOL;
                $strMSG .= "Content-type:text/html; charset=\"UTF-8\"".PHP_EOL;
                $strMSG .= "Content-Transfer-Encoding: 8bit" .PHP_EOL .PHP_EOL;
                $strMSG .= $strMessage .PHP_EOL;
                    //*** Attachment ***//  
                    for($x=0;$x<count($fname);$x++)  
                    {  
                        if($fname[$x] != "")  
                        {  
                            $strContent = chunk_split(base64_encode(file_get_contents($tmp_path[$x]))); 
                            $strMSG .= "--".$strSid.PHP_EOL;                    
                            $strMSG .= "Content-Type: ".$ftypes[$x]."; name=\"".$fname[$x]."\"".PHP_EOL;
                            $strMSG .= "Content-Transfer-Encoding: base64".PHP_EOL;
                            $strMSG .= "Content-Disposition: attachment; filename=\"".$fname[$x]."\"".PHP_EOL.PHP_EOL;
                            $strMSG .= $strContent.PHP_EOL;
                        }
                    }
                $strMSG .= "--".$strSid."--";

            }

            // sleep(3) 1 email in 3 sec, 20 emails in 1 min, 1200 email in 1 hour
            // sleep(4) 1 email in 4 sec, 15 emails in 1 min, 900 email in 1 hour
            sleep(4); 
            $flgSend = mail($strTo,utf8_decode($strSubject),$strMSG,$strHeader);  
            global $mailSended,$diff;
            $mailSended++;
                if($flgSend)  
                {  
                    echo "</br> $vasia Mail send completed." ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";  
                }  
                else  
                {  
                    echo "</br> $vasia Cannot send mail. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";  
                }
                $end_time = time();
                $diff = $end_time - $_SESSION['start_time'];
                echo "Sent " . $mailSended . " emails in " .secondsToTime($diff);   
                ob_flush();
        } 

    function secondsToTime($s)
    {
        $h = floor($s / 3600);
        $s -= $h * 3600;
        $m = floor($s / 60);
        $s -= $m * 60;
        return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);
    }

    ?> 

问题

  1. 如何在清除最后一个回声输出的同一行中显示浏览器回显输出?
  2. 在脚本运行之前如何防止浏览器超时?我想实时看到回音输出!!!

2 个答案:

答案 0 :(得分:0)

PHP不在浏览器中执行,它在服务器上执行,然后它的输出被发送到浏览器。一旦服务器发送脚本的输出,它就不再对它进行任何控制。因此,您不能用另一个“替换”一个回声。你需要Javascript来做这样的事情。

你的PHP没有超时。你在顶部有set_time_limit(0);,它删除了时间限制。如果某些事情超时,则不是PHP。

答案 1 :(得分:0)

如果你想在服务器上隐藏上一行等php渲染,我会回到Javascript。检查你php.ini并查看你的超时有时你无法覆盖这些设置。您正在寻找的设置是

max_execution_time
祝你好运。