Content · Programming · PHP

PHP ftell() Function: Usage and Examples

Understand PHP ftell() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use ftell() when you need to returns the current byte offset of a stream pointer in a PHP application. The examples below show the actual operation and its limitations.

What Is ftell() in PHP?

Returns the current byte offset of a stream pointer. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

ftell($stream): int|false

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

$stream = fopen("php://temp","w+"); fwrite($stream,"Hello"); echo ftell($stream); fclose($stream);

Output:

5

Example 2: Different Input or Behavior

<?php

$stream = fopen("php://temp","w+"); fwrite($stream,"ABCDE"); rewind($stream); fread($stream,2); echo ftell($stream); fclose($stream);

Output:

2

Practical Example: Applying the Function

<?php

$stream = fopen("php://temp","w+"); fwrite($stream,"HEADER:BODY"); rewind($stream); $header = fread($stream,7); echo "Body begins at byte " . ftell($stream); fclose($stream);

Output:

Body begins at byte 7

Important Behavior and Edge Cases

Offsets are bytes, not Unicode characters; stream wrappers may be non-seekable.

Comparison With Related Functions

ApproachDifference
ftell()Returns the current byte offset of a stream pointer.
Related operationfseek() moves the stream pointer; ftell() reads its current position.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationOffsets are bytes, not Unicode characters; stream wrappers may be non-seekable.
Confusing this function with a related operationfseek() moves the stream pointer; ftell() reads its current position.

Key Takeaways

  • Returns the current byte offset of a stream pointer.
  • Offsets are bytes, not Unicode characters; stream wrappers may be non-seekable.
  • fseek() moves the stream pointer; ftell() reads its current position.

Use ftell() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.