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
The ftell function has one mandatory parameter, which is the name of the file you wish to track the position of.
The ftell function returns the position of the pointer.
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.
<?php$file = fopen("main.txt","r");echo("Position before moving the pointer: ");echo(ftell($file));//moving the pointer to position 5fseek($file,"5");echo "\nPosition after moving the pointer: " . ftell($file);fclose($file);?>
Free Resources