PHP Function Reference

PHP ftell() Function



The PHP ftell() function returns the current position of the file read/write pointer in an open file.

Syntax

ftell(stream, length)

Parameters

stream Required. Specify the file pointer to check. It must be valid, and must point to a file successfully opened by fopen() or popen().

Return Value

Returns the position of the file pointer in an open file. If an error occurs, returns false.

Example:

Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, the file is opened using fopen() function with 'r' mode. While reading this file, this function is used to get the current position of the file pointer.

<?php
//open the file in read mode
$fp = fopen("test.txt", "r") or 
         die("Unable to open file!");

//file pointer at the beginning of the file
echo "At beginning: ".ftell($fp)."\n";

//reading first 14 bytes from the file
fgets($fp, 15);
echo "After 14 bytes: ".ftell($fp)."\n";

//reading first line from the file
fgets($fp);
echo "After first line: ".ftell($fp)."\n";

//reading second line from the file
fgets($fp);
echo "After second line: ".ftell($fp)."\n";

//close the file
fclose($fp);
?>

The output of the above code will be:

At beginning: 0
After 14 bytes: 14
After first line: 22
After second line: 48

❮ PHP Filesystem Reference