In PHP, ftell() returns the current position of the file pointer in an open file. The position is counted in bytes from the start of the file.
Reading or writing moves the pointer forward, and fseek() or rewind() can move it anywhere. ftell() simply tells you where it is right now, which is useful for remembering a spot and coming back to it.
Syntax
Call ftell() with the values you need. Start with this line.
PHP
$pos = ftell($fp);
Call
What it does
ftell($fp)
Returns the current byte position, for example 0 right after opening the file.
fread($fp, 5); ftell($fp)
Returns 5 after reading five bytes from the start.
$mark = ftell($fp)
Saves the position so you can return to it later with fseek().
ftell() counts bytes, not characters or lines. In a UTF-8 file, a single letter such as an accented e can use more than one byte. If the file was opened in append mode, the result can be unreliable.
Library declarations
PHP
ftell(resource $stream): int|false
Parameters
Parameter
Description
stream
Required. A file handle returned by fopen(). The file must still be open.
Return Value
Return Value
Description
int
The current position of the file pointer as a number of bytes from the start of the file.
false
Returned when an error occurs.
PHP Examples
Example 1 — Find the Position After Reading
Read a few bytes from a file and then ask ftell() how far the pointer has moved.
Mark: 5
Read: BBBB|
Back at mark, read again: BBBB
How It Works
The first fread() skips AAAA| and moves the pointer to byte 5.
ftell() saves that position in the $mark variable.
The second fread() reads BBBB| and moves the pointer on to byte 10.
fseek($fp, $mark) jumps back to byte 5, so reading 4 bytes gives BBBB again.
Frequently Asked Questions
It returns the current position of the file pointer, counted in bytes from the start of the file.
Use fseek() to jump to a position, or rewind() to go back to the start. ftell() only reports the position and never changes it.
Bytes. In UTF-8 text, letters outside the basic English set can use two or more bytes each.
In a or a+ mode the system writes at the end of the file no matter where the pointer is, so the reported position is not reliable. Use another mode when you need exact positions.
It is useful for remembering a spot in a large file so you can come back to it, or for tracking how many bytes you have already processed.