search
HomeBackend DevelopmentPHP Tutorialflush()函数无效?不能实时刷新输出,如何解决呢?

本帖最后由 zhoumengkang 于 2013-05-28 13:46:14 编辑

测试代码如下:
<?phpfor($i=0;$i<5;$i++){  	echo '<br/>abc___'.$i;	ob_flush();	flush();	sleep(1);}ob_end_flush();?> 

理论上应该是1秒出来一条是吧?但是我测试的结果是5秒之后才一起出来。
网上查了下有这样说的“个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止。 ”我这个是64位的windows的本地xampp的集成环境。

回复讨论(解决方案)

这种无端占用用户带宽的做法是很无聊的
延时输出的效果应在浏览器中用 js 实现

你的代码在 IE 中永远都不会看到期望的效果,因为 IE 需等待缓冲区装满后才解释收到的数据

这种无端占用用户带宽的做法是很无聊的
延时输出的效果应在浏览器中用 js 实现

你的代码在 IE 中永远都不会看到期望的效果,因为 IE 需等待缓冲区装满后才解释收到的数据
额,不是,我知识对这个函数不太了解。我用的chrome在测试。
其实我是想实现这样的效果:比如我写一个脚本批量处理一些文件
但那个过程可能很漫长,给别人用的话,别人都不知道是在执行呢,还是卡死了。
所以想每循环执行成功一次就输出一句xxxx执行成功;
-------------
最类似的就是咱们安装开源软件的时候见到的创建数据表的时候的显示。
但是我自己写的就会在数据表全部创建完之后才能显示。

这种无端占用用户带宽的做法是很无聊的
延时输出的效果应在浏览器中用 js 实现

你的代码在 IE 中永远都不会看到期望的效果,因为 IE 需等待缓冲区装满后才解释收到的数据
我的想法不是“延时”而是“即时”的显示我已完成的操作。不知道版主有什么高招。

同问!我现在也有一个程序要运行几个小时,一直运行,我只能去数据库查看数据来判断是否运行正常。
我的暂时做法只能做个简单的监控。比如在程序中加memcache写入操作,另外写个页面来读取。

同问!我现在也有一个程序要运行几个小时,一直运行,我只能去数据库查看数据来判断是否运行正常。
我的暂时做法只能做个简单的监控。比如在程序中加memcache写入操作,另外写个页面来读取。 有好的办法了,记得分享下 我去看看别的开源的代码在安装的时候是如何实现那个即时显示的效果的。

我又回来了,试试

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>

这两天出这个问题的还挺多的。
这个写法在各浏览器测试都没问题,但有童鞋说,这种做法往缓存区写垃圾,可以试试PHP抛出异常,有空研究一下再分享 。

我又回来了,试试

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>

这两天出这个问题的还挺多的。
这个写法在各浏览器测试都没问题,但有童鞋说,这种做法往缓存区写垃圾,可以试试PHP抛出异常,有空研究一下再分享 。
试了下,果然OK。机密是在哪里?

我又回来了,试试

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>

这两天出这个问题的还挺多的。
这个写法在各浏览器测试都没问题,但有童鞋说,这种做法往缓存区写垃圾,可以试试PHP抛出异常,有空研究一下再分享 。
完全是因为这个?echo str_pad('',4096);
第二个设置时限的可以省略,这个程序跑下来肯定不会超过30秒。
抛出异常,try-catch还可以这样用,学习了,我好好想想!你是一个好人!

嗯嗯,我的这个程序一开始考虑用php_cli,因为程序步骤多,觉得不友好,还是选择页面;但运行时间太长,开着页面反倒成了累赘,还是用cli吧,加油。

嗯嗯,我的这个程序一开始考虑用php_cli,因为程序步骤多,觉得不友好,还是选择页面;但运行时间太长,开着页面反倒成了累赘,还是用cli吧,加油。 今天又试了下用chrome测试,还是等运算完了,一次性输出。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.