What is the ftell() function in PHP?

Overview

The ftell function in PHP returns the read or write pointer’s current position. This means that if a file is open and one reads or writes, the pointer parsing the file has a position that changes, too. It is necessary that the file is open before using ftell.

The general syntax for the ftell function is as follows:

ftell(resource $stream): int|false 

Parameters

The ftell function has one mandatory parameter, which is the name of the file you wish to track the position of.

Return value

The ftell function returns the position of the pointer.

Example

The following example will help you understand the ftell function better. As shown below, we use the fseek function to move the pointer to position 5. Moreover, we use the ftell function to compare the position before using the fseek function to move the pointer, and then print after using the fseek function.

main.php
main.txt
<?php
$file = fopen("main.txt","r");
echo("Position before moving the pointer: ");
echo(ftell($file));
//moving the pointer to position 5
fseek($file,"5");
echo "\nPosition after moving the pointer: " . ftell($file);
fclose($file);
?>

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved