Working with File Pointers and Seeking in PHP
Working with file pointers and seeking in PHP is an essential operation for efficiently managing files. When you open a file, PHP uses an internal file pointer to keep track of the current position within the file. By default, the pointer starts at the beginning of the file and moves as you read or write data.
However, PHP also allows you to move this pointer manually using file seeking functions, which gives you more control over reading and writing data.
What is a File Pointer in PHP?
A file pointer is a reference or marker used by the operating system to track the current position within an open file to a specific location. This is especially useful for reading from or writing to a file from a specific point, rather than always starting from the beginning or end of the file.
When you open a file using fopen() function, a file pointer is automatically positioned at the beginning of the file. The fopen() function requires two arguments: the path to the file and the mode in which the file should be opened.
<?php
$file = fopen("myfile.txt", "r+");
?>The mode determines how the file will be accessed (e.g. reading, writing, appending, or both). If the file does not exist, it will only be created automatically when you use modes such as:
- “w” (write)
- “w+” (read/write)
- “a” (append)
- “a+” (read/append)
With “r” or “r+”, the file must already exist, otherwise, the fopen() will return false.
Functions for Working with File Pointers in PHP
Here are the commonly used functions to manage file pointers in PHP:
- ftell($file)
- fseek($file, $offset, $whence)
- rewind($file)
Let’s understand each function one by one.
ftell() Function in PHP
The ftell() function in PHP is a built-in function used to read the current position of the file pointer within an open file. When you open a file and read or write data, PHP keeps track of the position of the file pointer. The ftell() function tells you where the pointer is currently located in terms of bytes from the beginning of the file.
Basic Syntax
ftell(resource $file): int|falseParameters:
The ftell() function accepts one parameter:
- $file → The file pointer resource returned by fopen().
Return Value:
- Returns the current position of the file pointer in bytes from the beginning of the file.
- Returns false on failure.
Example: Opening a File and Checking Pointer Position
<?php
$file = fopen("myfile.txt", "r");
// Check current pointer position.
echo "Pointer position: " . ftell($file) . "\n";
// Read 10 characters from the file.
$data = fread($file, 10);
echo "Read data: " . $data . "\n";
// Check pointer position again.
echo "Pointer position after reading: " . ftell($file) . "\n";
fclose($file);
?>Output:
Pointer position: 0
Read data: Hello PHP!
Pointer after reading: 10
In the example, the file pointer starts at 0. After reading 10 characters, it moves to position 10.
fseek() Function in PHP
The fseek() function in PHP is a built-in function used to move the file pointer to a desired position within an open file. By default, when you open a file, the pointer starts at the beginning, but with fseek() you can manually move it anywhere you want—forward, backward, or relative to the current or end position.
Basic Syntax
fseek(resource $file, int $offset, int $whence = SEEK_SET): intParameters:
The fseek() function takes three parameters:
- $file → The file pointer resource returned by fopen() function.
- $offset → The number of bytes to move the pointer.
- $whence (optional) → The reference position from which the offset is applied. It can take three constants:
- SEEK_SET → Move relative to the beginning of the file (default).
- SEEK_CUR → Move relative to the current file pointer position.
- SEEK_END → Move relative to the end of the file.
Return Value:
- Returns 0 on success.
- Returns -1 on failure.
Example: Jump to a Specific Position with SEEK_SET
<?php
$file = fopen("myfile.txt", "r");
// Move the pointer 6 bytes from the beginning.
fseek($file, 6, SEEK_SET);
// Read the next 4 characters.
echo fread($file, 4);
// Close the open file.
fclose($file);
?>Output:
PHP!
In this example, the pointer skipped the first 6 characters (Hello ) and started reading from “PHP!”.
Example: Move Pointer Relative to Current Position with SEEK_CUR.
<?php
$file = fopen("myfile.txt", "r");
// Read the first 5 characters.
echo fread($file, 5) . "\n";
// Skip 1 character from current position (the space).
fseek($file, 1, SEEK_CUR);
// Read the next 4 characters.
echo fread($file, 4);
fclose($file);
?>Output:
Hello
PHP!
Example: Move Pointer from End of File with SEEK_END
<?php
$file = fopen("myfile.txt", "r");
// Move 4 bytes before the end of file.
fseek($file, -4, SEEK_END);
// Read the last 4 characters from the file.
echo fread($file, 4);
fclose($file);
?>Output:
PHP!
In this example, the pointer is positioned four bytes before the end of the file and read the last four characters.
PHP rewind() Function
The rewind() function is another important function for working with file pointers in PHP. This is a built-in function used to reset the file pointer to the beginning of the file. In other words, the rewind() function moves the file pointer back to the beginning of an open file.
Whenever you read or write a file, the file pointer keeps moving forward. If you want to start again from the beginning without closing and reopening the file, you can simply call the rewind() function. This is equivalent to calling fseek($file, 0).
Basic Syntax
rewind(resource $file): boolParameters:
The rewind() function accepts one parameter:
- $file → The file pointer resource returned by fopen() function.
Return Value:
- Returns true on success.
- Returns false on failure.
Example:
<?php
$file = fopen("myfile.txt", "r");
// Read the first five characters.
echo fread($file, 5) . "\n";
// Move the pointer back to beginning within the file.
rewind($file);
// Read the first five characters again.
echo fread($file, 5);
fclose($file);
?>Output:
Hello
Hello
In this example, the pointer reads “Hello” at first. Then, the rewind($file) function moves the pointer back to the beginning (0) of the file. The second fread() function reads “Hello” again.
Conclusion
Working with file pointers and seeking in PHP gives you more control over file operations. By using PHP functions like ftell(), fseek(), and rewind(), you can efficiently read, write, and modify files at specific positions.




