英文:
How to properly flush PHP echo output
问题
I am using PHPMailer to send emails with attachments.
Each email takes about 5 seconds to send, and the PHP can send up to 5 emails with a total in that case of 10 PDFs...
During all that time, the browser shows a white page.
<?php
echo '<div>some spinner html</div>';
// code here to send emails
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
echo 'all the rest of the html';
?>
This doesn't work...
I have the feeling I can use an ob_start()
to buffer the rest of the page and only show a spinner.
英文:
I am using PHPMailer to send emails with attachments.
Each email takes about 5 seconds to send, and the PHP can send up to 5 emails with a total in that case of 10 PDFs...
During all that time, the browser shows a white page.
<?php
echo '<div>some spinner html</div>';
// code here to send emails
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
echo 'all the rest of the html';
?>
This doesn't work...
I have the feeling I can use an ob_start()
to buffer the rest of the page and only show a spinner.
答案1
得分: 2
Real answer: 使用 JS
并使用 PHP
创建 API
解决您的问题的最佳方案是使用 JavaScript
和 XHR
,为其创建一个漂亮的用户界面。如果您想在发送电子邮件后显示加载,然后在加载消失后显示其他内容,这只能通过 JS
实现。PHP
只是一种服务器端语言,能够向浏览器发送响应,无法访问已呈现的页面!
刷新输出
只需使用 ob_flush()
和 flush()
函数:
echo '<div>some spinner html</div>';
ob_flush();
flush();
// 这里是发送电子邮件的代码
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
ob_flush();
flush();
} else {
echo 'Message sent!';
ob_flush();
flush();
}
echo '所有其他的 HTML';
使用隐式刷新
这看起来像上一个例子,但减少了一个函数调用。这去掉了 flush()
调用!
ob_implicit_flush(true);
echo '<div>some spinner html</div>';
ob_flush();
// 这里是发送电子邮件的代码
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
ob_flush();
} else {
echo 'Message sent!';
ob_flush();
}
echo '所有其他的 HTML';
关闭输出缓冲区
另一种解决方案是关闭输出缓冲区:
while (@ob_end_flush());
echo '<div>some spinner html</div>';
// 这里是发送电子邮件的代码
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
echo '所有其他的 HTML';
英文:
Real answer: Use JS
and create an API using PHP
Best solution to your problem is using JavaScript
and XHR
and make a beautiful UI for it. If you want to show a loading and remove it after emails sent and show other stuff after disappearance of loading, it's only possible through JS
. PHP
is just a server side language capable of sending a response to browser and have no access to rendered page!
Flush output
Just use ob_flush()
and flush()
functions:
echo '<div>some spinner html</div>';
ob_flush();
flush();
// code here to send emails
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
ob_flush();
flush();
} else {
echo 'Message sent!';
ob_flush();
flush();
}
echo 'all the rest of the html';
Using implicit flush
This looks like the previous one, but reduced a function call. this removes the flush()
call!
ob_implicit_flush(true)
echo '<div>some spinner html</div>';
ob_flush();
// code here to send emails
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
ob_flush();
} else {
echo 'Message sent!';
ob_flush();
}
echo 'all the rest of the html';
Turn off output buffer
Other solution is to turn off output buffer:
while (@ob_end_flush());
echo '<div>some spinner html</div>';
// code here to send emails
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
echo 'all the rest of the html';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论