PHP

PHP SplFileObject Examples7 min read

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:

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

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:

Parameters: This function does not accept any parameter.

Return values: Returns TRUE on Success.

Below Programs illustrate the SplFileObject::eof() function in PHP.

Example:

Example:

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:

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

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:

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

 

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:

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

 

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:

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.

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:

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.

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:

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.

 

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:

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.

 

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:

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.

 

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:

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.

 

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:

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.

 

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:

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.

 

Leave a Comment