PHP SplFileObject fseek() Function Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The SplFileObject::fseek() function is an inbuilt function of Standard PHP Library (SPL) in PHP that allows you to move the file pointer to a specified position within a file opened using SplFileObject. The file pointer is the position where the next read or write operation will occur. This function is used to navigate within a file and read or write data from a specific location. Syntax: public SplFileObject::fseek(int $offset, int $whence = SEEK_SET): intParameters: This function accepts two parameters that are described below. $offset: The offset value represents the number of bytes to move the file pointer. It can be positive or negative, depending on the direction you want to move the pointer.$whence: This is an optional parameter that specifies the starting point for the offset calculation. It can take one of the following values: SEEK_SET - Set position equal to offset bytes, SEEK_CUR - Set position to current location plus offset, SEEK_END - Set position to end-of-file plus offset.Program 1: The following program demonstrates the SplFileObject::fseek() function. Save this file name as "output.txt" before running this program. Line 1: This is the first line of data.Line 2: This is the second line of data.Line 3: This is the third line of data. PHP <?php $file = new SplFileObject('./output.txt', 'r'); // Move the file pointer to the 10th byte // from the beginning of the file. $file->fseek(10, SEEK_SET); // Read the content from the current position //(10th byte) to the end of the file. $content = $file->fread($file->getSize() - $file->ftell()); echo $content; ?> Output: is is the first line of data.Line 2: This is the second line of data.Line 3: This is the third line of data.Program 2: The following program demonstrates the SplFileObject::fseek() function. Save this file name as "output.txt" before running this program. Ram SeetaMichael JohnsonEmily BrownWilliam Davis PHP <?php $file = new SplFileObject('output.txt', 'r'); // Move the file pointer to the beginning // of the third line (name) $file->fseek(strlen("Michael \n") * 2, SEEK_SET); // Read the third name from the current position. $thirdName = trim($file->fgets()); // Output the third name. echo "The third name in the file is: " . $thirdName; ?> Output: The third name in the file is: Johnson Reference: https://www.php.net/manual/en/splfileobject.fseek.php Comment More infoAdvertise with us Next Article PHP | SplFileObject fread() Function N neeraj3304 Follow Improve Article Tags : PHP PHP-function PHP-SplFileInfo Similar Reads 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: array SplFileObject::fstat( void ) Parameters: This function does not accept any parameter. Return values: This function returns an array which c 1 min read PHP | SplFileObject fgets() Function The SplFileObject::fgets() function is an inbuilt function of the Standard PHP Library (SPL) in PHP which is used to get a line from the file. Syntax: string SplFileObject::fgets( void ) Parameters: This function does not accept any parameter. Return values: This function returns a string contain 1 min read PHP | 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: string SplFileObject::fread( $length ) Parameters: This function accepts single parameter $length which is used to specify the lengt 1 min read 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: string SplFileObject::fgetc( void ) Parameters: This function does not accept any parameter. Return values: Returns single character read from the file or 1 min read 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: int SplFileObject::ftell( void ) Parameters: This function does not accept any parameters. 1 min read PHP | SplFileObject flock() Function The SplFileObject flock() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to apply portable lock on the file. Syntax: bool SplFileObject::flock( $opr, $isBlock ) Parameters: This function accept two parameters as mentioned above and described below: $opr: It is use 2 min read Like