PHP ftell() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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);
CallWhat 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

ParameterDescription
streamRequired. A file handle returned by fopen(). The file must still be open.

Return Value

Return ValueDescription
intThe current position of the file pointer as a number of bytes from the start of the file.
falseReturned 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.

PHP
<?php
$path = sys_get_temp_dir() . '/ftell_demo.txt';
file_put_contents($path, 'Hello World');

$fp = fopen($path, 'r');

echo "Start: ", ftell($fp), PHP_EOL;
fread($fp, 5);
echo "After reading 5 bytes: ", ftell($fp), PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. A freshly opened file in read mode has the pointer at position 0.
  2. fread($fp, 5) reads the letters Hello and moves the pointer forward by 5 bytes.
  3. ftell() now returns 5.
  4. The file is closed and the temporary file is removed.

Example 2 — Compare the End Position with the File Size

After reading the whole file, the pointer sits at the end. That position equals the file size in bytes.

PHP
<?php
$path = sys_get_temp_dir() . '/ftell_edge.txt';
file_put_contents($path, 'abc');

$fp = fopen($path, 'r');
echo "Start: ", ftell($fp), PHP_EOL;

fgets($fp);
echo "End: ", ftell($fp), PHP_EOL;
echo "Same as file size: ", ftell($fp) === filesize($path) ? 'yes' : 'no', PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. The file holds the three characters abc, so its size is 3 bytes.
  2. fgets() reads to the end of the line, and with no newline that means the whole file.
  3. The pointer is now at byte 3, which is the end of the file.
  4. Comparing with filesize() confirms that ftell() and the file size match.

Example 3 — Remember a Position and Come Back

Save the pointer with ftell(), read ahead, then jump back with fseek() to read the same bytes again.

PHP
<?php
$path = sys_get_temp_dir() . '/ftell_mark.txt';
file_put_contents($path, 'AAAA|BBBB|CCCC');

$fp = fopen($path, 'r');

fread($fp, 5);
$mark = ftell($fp);
echo "Mark: ", $mark, PHP_EOL;

echo "Read: ", fread($fp, 5), PHP_EOL;

fseek($fp, $mark);
echo "Back at mark, read again: ", fread($fp, 4), PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. The first fread() skips AAAA| and moves the pointer to byte 5.
  2. ftell() saves that position in the $mark variable.
  3. The second fread() reads BBBB| and moves the pointer on to byte 10.
  4. 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.

Next: ftruncate()

ftruncate() cuts an open file down to a given size, or makes it larger by adding empty byt.

ftruncate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful