The SplFileObject::fread() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to reads the given number of bytes from the file.
Syntax:
string SplFileObject::fread( $length )
Parameters: This function accepts single parameter $length which is used to specify the length to be read from file in bytes.
Return values: This function returns the string read from the file on success or false on failure.
Note: It make sure the file used in below program named as gfg.txt should have read permissions.
Below Programs illustrate the SplFileObject::fread() function in PHP:
Program 1:
<?php // Create an Object $file = new SplFileObject("gfg.txt", "r"); // Read 5 bytes from file $gfg = $file->fread(5); // Print Result echo ($gfg); ?> |
Output:
Geeks
Program 2:
<?php // PHP program to use array to check // multiple files $GFG = array( "dummy.txt", "gfg.txt", "frame.txt" ); foreach ($GFG as &$file_name) { // Create new SplFile Object $file = new SplFileObject($file_name, "r"); $contents = $file->fread(13); echo $contents . "</br>"; } ?> |
Output:
GeeksforGeeks GeeksforGeeks GeeksforGeeks
Reference: http://php.net/manual/en/splfileobject.fread.php
Recommended Posts:
- PHP | fread( ) Function
- PHP | SplFileObject current( ) Function
- PHP | SplFileObject fgetc() Function
- PHP | SplFileObject eof() Function
- PHP | SplFileObject fgets() Function
- PHP | SplFileObject fgetss() Function
- PHP | SplFileObject setMaxLineLen() Function
- PHP | SplFileObject flock() Function
- PHP | SplFileObject getMaxLineLen() Function
- PHP | SplFileObject fwrite() Function
- PHP | SplFileObject ftruncate() Function
- PHP | SplFileObject ftell() Function
- PHP | SplFileObject fstat() Function
- PHP | SplFileObject fputcsv() Function
- PHP | SplFileObject seek() Function
- PHP | SplFileObject rewind() Function
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

