PHP SplFileObject Standard Library
In this post, you will learn about the SplFileObject Standard Library of the PHP programming language.
PHP SplFileObject is a Standard PHP Library available from PHP version 5.1 (PHP 5 >= 5.1.0, PHP 7). It provides an Object Oriented Interface for file handling. It provides collection of interfaces and classes that are meant to solve common problems.
Advantages of SplFileObject over file_get_contents()
As we know, PHP also has a file_get_contents() method to deal with files. So, why and where should we use SplFileObject instead of file_get_contents(). The file_get_contents() method is not suitable for manipulating large files. It has some limited functionality when dealing with files. But SplFileObject provides an object-oriented approach to dealing with files. By using this, we can handle file manipulation by creating a class and extending SplFileObject. It contains several inbuilt methods and properties.
PHP splfileobject Example
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
?>
In the above code, we have instantiated the SplFileObject.
Read File Contents using SplFileObject
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
foreach ($file as $line) {
echo $line;
}
?>
The above example opens the current file and iterates over the content line by line.
PHP SplFileObject fread()
The fread() function is an inbuilt function of Standard PHP Library (SPL) which is used to read the given number of bytes from the file. Syntax -
string SplFileObject::fread( $length )
This function accepts single parameter $length which is used to specify the length to be read from file in bytes and returns the string read from the file on success or false on failure.
The given PHP program illustrate the SplFileObject::fread() function in PHP.
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
$gfg = $file->fread(5);
?>
PHP SplFileObject eof()
The eof() method is used to check the end of the file. It does not accept any parameter and always returns a Boolean value, i.e., either TRUE or FALSE. Syntax-
string SplFileObject::eof( void )
The given PHP program illustrate the SplFileObject::eof() function.
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
foreach ($file as $line) {
while ( ! $file->eof()) {
echo $line;
}
}
?>
PHP SplFileObject current()
The current() method is used to retrieve the current line of the file.
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
foreach ($file as $line) {
echo ($file->key() + 1) . ': ' . $file->current();
}
?>
PHP SplFileObject fgets()
The SplFileObject::fgets() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get line number from the file. Syntax-
string SplFileObject::fgets( void )
This function does not accept any parameter. It returns an string containing the next line from the file return FALSE otherwise. The given PHP program illustrate the SplFileObject::fgets() function.
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
foreach ($file as $line) {
echo $file->fgets();
}
?>
PHP SplFileObject getMaxLineLen()
The getMaxLineLen() method returns the maximum line length of the file if it is set in setMaxLineLen(), otherwise by default it returns 0.
<?php
$filename = "demo.txt";
$file = new SplFileObject($filename);
var_dump($file->getMaxLineLen());
$file->setMaxLineLen(10);
var_dump($file->getMaxLineLen());
?>
Related Articles
PHP User Authentication by IP AddressHow to encrypt password in PHP
Different datatype comparison in PHP
PHP loop through an associative array
PHP CURL Cookie Jar
PHP remove last character from string
PHP calculate percentage of total
Insert image in database using PHP
PHP set a cookie to store login detail
How to create search filter in PHP
Getting Document of Remote Address
PHP Connection and File Handling on FTP Server
Simple File Upload Script in PHP
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
Simple PHP File Cache