The ftell Function

The ftell function returns the current position of the pointer in a file stream. The position is specified in bytes from the beginning of the file. The function takes one parameter - a pointer to an open file resource.

Syntax

phpftell(resource $handle): int|false

Example

Determine the current position in a newly opened file:

php<?php
  $file = fopen('test.txt', 'r');
  echo ftell($file);
  fclose($file);
?>

Code execution result:

php0

Example

Determine the position after reading several bytes:

php<?php
  $file = fopen('test.txt', 'r');
  fread($file, 5);
  echo ftell($file);
  fclose($file);
?>

Code execution result:

php5

Example

Check the position after moving the pointer:

php<?php
  $file = fopen('test.txt', 'r');
  fseek($file, 10);
  echo ftell($file);
  fclose($file);
?>

Code execution result:

php10

See Also

  • the fseek function,
    which moves the file pointer
  • the rewind function,
    which resets the file pointer
  • the filesize function,
    which returns the file size