The Standard PHP Library (SPL) is a collection of interfaces and classes that are meant to solve common problems.
The SplFileObject is available and compiled by default in PHP 5.1.0 and it offers an object oriented interface for a file.
SplFileObject fread() Function
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:
1 2 3 |
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.
Example: Below Programs illustrate the SplFileObject::fread() function in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // Create an Object $file = new SplFileObject("data.txt", "r"); // Read 10 bytes from file $data = $file->fread(10); // Print Result echo ($data); ?> |
SplFileObject eof() Function
The SplFileObject::eof() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used reached end of file.
Syntax:
1 2 3 |
string SplFileObject::eof( void ) |
Parameters: This function does not accept any parameter.
Return values: Returns TRUE on Success.
Below Programs illustrate the SplFileObject::eof() function in PHP.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php // Creating SplFile Object $file = new SplFileObject(__FILE__);//this file foreach ($file as $data => $line) { //read some bytes if($file->eof() == true) { echo "Reached EOF"; break; } } ?> |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php // PHP program to use array to check // multiple files $list = array( "/library/functions.php", "list.txt", "myData.txt" ); foreach ($list as &$fileName) { // Create new SplFile Object $file = new SplFileObject($fileName); foreach($file as $lines) { if($file->eof() == true) echo "Reached EOF" . "</br>"; } } ?> |
SplFileObject ftruncate() Function
The SplFileObject::ftruncate() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to truncates the file size in bytes.
Syntax:
1 2 3 |
bool SplFileObject::ftruncate( $length ) |
Parameters: This function accept single parameter $length which specified the length of truncate of the file.
Return values: This function returns True on success or False on failure.
Example: Below Programs illustrate the SplFileObject ftruncate() function in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $file = new SplFileObject("data.txt", "w+"); $file->fwrite("Some Text"); // Truncate file $file->ftruncate(5); // Rewind and reading data from file $file->rewind(); // Print result after truncate echo $file->fgets(); ?> |
SplFileObject ftell() Function
The SplFileObject::ftell() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to return the position of the file pointer which specifies the current offset in the file.
Syntax:
1 2 3 |
int SplFileObject::ftell( void ) |
Parameters: This function does not accept any parameters.
Return values: This function returns the position of the file pointer.
Example: Below Programs illustrate the SplFileObject::ftell() function in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // Create an SplObject $file = new SplFileObject("data.txt"); // Read line from file $data = $file->fgets(); // Print position echo $file->ftell(); ?> |
SplFileObject fstat() Function
The SplFileObject::fstat() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to give the information of the file.
Syntax:
1 2 3 |
array SplFileObject::fstat( void ) |
Parameters: This function does not accept any parameter.
Return values: This function returns an array which contains details of the file.
Example: Below Programs illustrate the SplFileObject::fstat() function in PHP
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php // Create an SplFile Object $file = new SplFileObject("data.txt", "r"); $stat = $file->fstat(); // Print result print_r($stat); ?> |
SplFileObject fgetss() Function
The SplFileObject::fgetss() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get line from file and strip HTML tags.
Syntax:
1 2 3 |
string SplFileObject::fgetss( $tags) |
Parameters: This function accept only one parameter $tags an optional parameter to specify tags which should not be stripped.
Return values: This function returns the next line of the file with HTML and PHP code stripped return FALSE otherwise.
Example: Below Programs illustrate the SplFileObject::fgets() function in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $world = "World"; $str = <<<EOF <p>Hello</p> <p>$world</p> EOF; file_put_contents("data.txt", $str); $file = new SplFileObject("data.txt"); while (!$file->eof()) { echo $file->fgetss(); } ?> |
SplFileObject rewind() Function
The SplFileObject::rewind() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to Rewind the file to the first line.
Syntax:
1 2 3 |
void SplFileObject::rewind( $line_num) |
Parameters: This function does not accept any parameter.
Return values: This function does not return any value.
Example: Below Programs illustrate the SplFileObject::rewind() function in PHP.
1 2 3 4 5 6 7 8 9 10 |
<?php $file = new SplFileObject('data.txt','r'); $file->rewind(); echo $file->current(); ?> |
SplFileObject seek() Function
The SplFileObject::seek() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to seek to specify the line.
Syntax:
1 2 3 |
void SplFileObject::seek( $line_num) |
Parameters: This functions accept only one parameter $line_num which specifies the line number of the file.
Return values: This function does not return any value.
Example: Below Programs illustrate the SplFileObject::seek () function in PHP.
1 2 3 4 5 6 7 8 9 10 |
<?php $file = new SplFileObject('data.txt','r'); $file->seek(2); echo $file->current(); ?> |
SplFileObject fputcsv() Function
The SplFileObject fputcsv() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used write a field array as a CSV line.
Syntax:
1 2 3 |
string SplFileObject::fputcsv() |
Parameters: This function accept four parameters one is mandatory and three are optional.
- $fields: Specifies the an array of values.
- $delimiter: An optional parameter which specify sets the field delimiter.
- $enclosure: An optional parameter which specify field enclosure.
- $escape: An optional parameter used for escape character.
Return values: This function returns length of the written string or FALSE otherwise.
Example: Below Program illustrate the SplFileObject fputcsv() function in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php // Create an Array $list = array ( array('Pencil', 'Book', 'Sample', 'Post'), array('"Some"', '"Text"') ); // Creating Spl Object $file = new SplFileObject('data.csv', 'w'); foreach ($list as $arr) { $file->fputcsv($arr); } echo "Successfully write data in data.csv"; ?> |
SplFileObject current( ) Function
The SplFileObject::current() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get current line of file.
Syntax:
1 2 3 |
string SplFileObject::current( void ) |
Parameters: This function does not accept any parameter.
Return values: Returns current line of the file.
Example: Below Programs illustrate the SplFileObject::current() function in PHP.
1 2 3 4 5 6 7 8 9 10 11 |
<?php // Creating SplFile Object $file = new SplFileObject("data.txt"); foreach ($file as $line) { echo $file->key() + 1 ." Line".':> '.$file->current(); } ?> |
SplFileObject fgetc() Function
The SplFileObject::fgetc() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get character from file.
Syntax:
1 2 3 |
string SplFileObject::fgetc( void ) |
Parameters: This function does not accept any parameter.
Return values: Returns single character read from the file or FALSE on EOF.
Example: Below Programs illustrate the SplFileObject::fgetc() function in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // Create SplFileObject object $file = new SplFileObject("data.txt"); // Print all characters of file while (false !== ($d = $file->fgetc())) { echo "$d"; } ?> |
SplFileObject fgets() Function
The SplFileObject::fgets() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is usd to get line from file.
Syntax:
1 2 3 |
string SplFileObject::fgets( void ) |
Parameters: This function does not accept any parameter.
Return values: This function returns an string containing the next line from the file return FALSE otherwise.
Example: Below Programs illustrate the SplFileObject::fgets() function in PHP.
1 2 3 4 5 6 7 8 9 10 |
<?php // Creating SplFile Object $data = new SplFileObject("data.txt"); while (!$data->eof()) { echo $data->fgets()."<br>"; } ?> |
SplFileObject fwrite() Function
The SplFileObject::fwrite() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to write to the file.
Syntax:
1 2 3 |
int SplFileObject::fwrite( $str, $length ) |
Parameters: This function accept two parameters as mention above and describe below:
- $str: It is used to specify the string which need to write to the file.
- $length: It is an optional parameter. If it is given then file writing will stop after a specified length.
Return values: This function returns the number of bytes written to the file and 0 on error.
Example: Below Programs illustrate the SplFileObject::fwrite() function in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php // Create a file named "data.txt" if not exist $data = new SplFileObject("data.txt", "w+"); // Write data in data.txt $data->fwrite("Web HTML CSS JS"); // Open file again in read mode $data = new SplFileObject("data.txt"); // Print result after written while (!$data->eof()) { echo $data->fgetc(); } ?> |