PHP Filesystem fflush() Function
The PHP Filesystem fflush() function is used to flush, or clear, the output buffer. It is commonly used with files or network streams to make sure that any data added to them is actually transported or stored.
The fflush() function writes all the buffered output to an open file. This function forces a write of all the buffered output to resource pointed to by the file handle. It can return true on success or false on failure.
Syntax
Below is the syntax of the PHP Filesystem fflush() function −
bool fflush ( resource $handle )
Parameters
The parameters are needed to use the fflush() function is mentioned below −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
handle(Required) It is the file pointer or resource you want to flush. |
Return Value
It returns TRUE on success, or FALSE on failure.
PHP Version
The fflush() function was first introduced as part of core PHP 4.0.1 and work well with the PHP 5, PHP 7, PHP 8.
Example
In this example we will try to flush any buffered output using the PHP Filesystem fflush() function in PHP.
<?php
$file = fopen("/PhpProject/sample.txt", "r+");
// some code
fflush($file);
echo $file;
fclose($file);
?>
Output
This will produce the following result −
Resource id #5
Example
This PHP code shows how you can open a file for writing, write data to the file and make sure the data is properly written, and then close the file.
<?php
$handle = fopen("myfile.txt", "w");
fwrite($handle, "Hello!");
// Flush the buffer to ensure data is written to the file
fflush($handle);
fclose($handle);
echo "The file has been successfully written and closed.";
?>
Output
This will generate the below result −
The file has been successfully written and closed.
Example
This example shows us how we can use fflush() to immediately display output.
<?php echo "Hello, "; // Flush the output buffer to display "Hello, " immediately fflush(); echo "world!"; ?>
Output
This will create the below outcome −
Hello, world!
Example
This example shows us how we can use fflush() to make sure data is written to a file immediately.
<?php
$file = fopen("output.txt", "w");
fwrite($file, "Hello, ");
// Flush the output buffer to make sure data is written to the file
fflush($file);
fwrite($file, "Tutorialspoint!");
fclose($file);
?>
Output
This will lead to the following outcome −
Hello, Tutorialspoint!
Example
This example shows us how to use fflush() with implicit output buffering.
<?php ob_start(); echo "Hello, "; // Flush the output buffer to display "Hello, " immediately fflush(); echo "Everyone!"; ob_end_flush(); ?>
Output
The result of this PHP code is −
Hello, Everyone!
Note
It is commonly used when writing data to files or network ports to make sure that the data is written quickly rather being delayed in memory.
Summary
The fflush() is a PHP function for clearing the output buffer. It ensures that all data written to a file or network stream is quickly sent or stored. It takes a single parameter, which is a file pointer or resource identifying the connection to the file or stream to flush. After flushing, it returns true if successful and false otherwise. It is commonly used to verify that data is written directly to files or network ports.