PHP rewind() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, rewind() moves the file pointer back to the very beginning of an open file or stream.

After you read or write, the pointer moves forward. Call rewind() when you want to read the same data again from the start without closing and reopening the file.

Syntax

Call rewind() with the values you need. Start with this line.

PHP
$ok = rewind($handle);
CallWhat it does
rewind($handle)Moves the pointer to position 0 and returns true.
fseek($handle, 0)Does the same job as rewind() for most files.
rewind($handle); ftell($handle)Moves to the start and then confirms that the position is 0.

rewind() works only on streams that can seek, such as normal files and php://memory. It does not work on things like network sockets. For a file opened in append mode, writes still go to the end even after rewind().

Library declarations
PHP
rewind(resource $stream): bool

Parameters

ParameterDescription
streamRequired. A file pointer returned by fopen(), tmpfile() or a similar function. It must be a valid open stream.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Write, Rewind, Then Read

Write some text into memory and read it back from the start.

PHP
<?php
$handle = fopen('php://memory', 'w+');

fwrite($handle, 'Hello');
rewind($handle);

echo fread($handle, 5), PHP_EOL;

fclose($handle);

How It Works

  1. php://memory opens a temporary stream that lives only in memory.
  2. fwrite() writes Hello, which moves the pointer to the end.
  3. rewind() puts the pointer back to position 0.
  4. fread() now reads the five characters from the start and echo prints them.

Example 2 — Rewind in Append Mode

In append mode, rewind() helps you read from the start, but writing still goes to the end of the file.

PHP
<?php
$file = sys_get_temp_dir() . '/rewind_demo.txt';
file_put_contents($file, 'A');

$handle = fopen($file, 'a+');

fwrite($handle, 'B');
rewind($handle);
echo stream_get_contents($handle), PHP_EOL;

fwrite($handle, 'C');
fclose($handle);

echo file_get_contents($file), PHP_EOL;
unlink($file);

How It Works

  1. The file starts with the letter A, and a+ opens it for reading and appending.
  2. After writing B, rewind() lets us read the whole file from the start, which gives AB.
  3. The next fwrite() adds C. Append mode always writes at the end, even though the pointer was at the start.
  4. The final file content is ABC.

Example 3 — Count Lines, Then Read Again

Loop through a file once to count lines, then rewind and read the first line.

PHP
<?php
$file = sys_get_temp_dir() . '/fruits.txt';
file_put_contents($file, "apple\nbanana\ncherry\n");

$handle = fopen($file, 'r');

$count = 0;
while (fgets($handle) !== false) {
    $count++;
}
echo "Lines: $count", PHP_EOL;

rewind($handle);
echo 'First: ', trim(fgets($handle)), PHP_EOL;

fclose($handle);
unlink($file);

How It Works

  1. The file is created with three lines of text.
  2. The while loop reads line by line until fgets() returns false at the end of the file.
  3. rewind() moves the pointer back to the beginning.
  4. fgets() now reads the first line again, and trim() removes the line break before it is printed.

Frequently Asked Questions

It moves the file pointer back to the start of an open file or stream so you can read it again.
For most files, yes. rewind() is a shorter way to write the same thing and returns true or false.
Writing moves the pointer to the end. Call rewind() first so fread() starts from the beginning.
It moves the read position, but writes in append mode always go to the end of the file, no matter where the pointer is.
No. The stream must support seeking. Normal files and php://memory do, while sockets and some pipes do not.

Next: rmdir()

rmdir() removes an empty folder from the file system.

rmdir() 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