请教PHP fflush 的问题, not working
有没有用过 fflush
函数
官方解释是这样说的:
Flushes the output to a file
This function forces a write of all buffered output to the resource pointed to by the file handle.
也就是说把文件的写入缓存强行释放输出到文件指针.
http://php.net/manual/en/function.fflush.php
但我发现 fwrite 在执行完之后, 内容就马上写入文件了. 不需要fflush, 也不需要fclose, 对这个问题感到很疑惑, fflush 究竟有提供了什么作用.
代码:
<?php $fp = fopen("fflush.txt", "w+"); for ($i = 0; $i < 10; $i++) { fwrite($fp, "Write something here\n"); // 马上写入内容, 根本不存在 buffer 一说啊! sleep(1); } fflush($fp); fclose($fp);
回复内容:
有没有用过 fflush
函数
官方解释是这样说的:
Flushes the output to a file
This function forces a write of all buffered output to the resource pointed to by the file handle.
也就是说把文件的写入缓存强行释放输出到文件指针.
http://php.net/manual/en/function.fflush.php
但我发现 fwrite 在执行完之后, 内容就马上写入文件了. 不需要fflush, 也不需要fclose, 对这个问题感到很疑惑, fflush 究竟有提供了什么作用.
代码:
<?php $fp = fopen("fflush.txt", "w+"); for ($i = 0; $i < 10; $i++) { fwrite($fp, "Write something here\n"); // 马上写入内容, 根本不存在 buffer 一说啊! sleep(1); } fflush($fp); fclose($fp);
给你参考一个方法 stream_set_write_buffer
,默认的 buffer_size
是 8K
,也就是说,fwrite
如果一次写入的数据量大于 buffer_size
,会拆分写入。这种情况在多进程操作同一文件时需要考虑,通常情况下 fflush
是没多大作用的,不过加了也无所谓,毕竟 fopen
还能打开其他的 文件
似的资源。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
