PHP SplFileObject fpassthru() Function
Last Updated :
18 Sep, 2023
The SplFileObject::fpassthru() is an inbuilt function in PHP that is used to output the contents of a file to the output buffer, typically the browser, without reading the entire file into memory. It allows you to efficiently stream the contents of a file in smaller chunks, which is particularly useful for working with large files.
Syntax:
public SplFileObject::fpassthru(): int
Parameters: This function does not have any parameters.
Return Value: This function returns the number of characters from the buffer.
Program 1: The following program demonstrates SplFileObject::fpassthru() function.
Note: Before running this program save this file as “data.txt”
John,Doe,25
Jane,Smith,30
Alice,Johnson,28
PHP
<?php
$file = new SplFileObject( 'data.txt' , 'r' );
$file -> fpassthru ();
?>
|
Output:
John,Doe,25
Jane,Smith,30
Alice,Johnson,28
Program 2: The following program demonstrates SplFileObject::fpassthru() function.
PHP
<?php
$filePath = 'output.txt' ;
if ( file_exists ( $filePath )) {
$file = new SplFileObject( $filePath , 'r' );
$file -> fpassthru ();
} else {
echo "File not found." ;
}
?>
|
Output:
hey Geeks for Geeks
Reference: https://www.php.net/manual/en/splfileobject.fpassthru.php
Share your thoughts in the comments
Please Login to comment...