PHP SplFileObject fpassthru() Function
Last Updated : 28 Apr, 2025
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'; // Check if the file exists if (file_exists($filePath)) { $file = new SplFileObject($filePath, 'r'); // Output the file content to the browser $file->fpassthru(); } else { // Handle the case when the file doesn't exist echo "File not found."; } ?>
Output:
hey Geeks for Geeks
Reference: https://www.php.net/manual/en/splfileobject.fpassthru.php