Thursday, October 16, 2025
HomeLanguagesPHP | SplFileObject ftell() Function

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.

Return values: This function returns the position of the file pointer.

Below Programs illustrate the SplFileObject::ftell() function in PHP:

Program 1:




<?php
  
// Create an Spl Object
$file = new SplFileObject("gfg.txt");
  
// Read line from file
$data = $file->fgets();
  
// Print position 
echo $file->ftell();
?>


Output:

13

Program 2:




<?php
   
// Create an Array
$GFG = array(
    "dummy.txt",
    "gfg.txt",
    "frame.txt"
);
   
// Loop to read each file
foreach ($GFG as &$arr) {
      
    // Creating Spl Object
    $file = new SplFileObject($arr);
       
    // Function to read first line
    $data = $file->fgets();
       
    // Display result
    echo $file->ftell() . "</br>";
}
?>


Output:

13
15
13

Reference: http://php.net/manual/en/splfileobject.ftell.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS