W3docs

rewind()

Learn how to use PHP's rewind() function to reset the file pointer to the beginning of an open file, with syntax, examples, and common pitfalls.

Introduction

When you read from a file in PHP, an internal file pointer keeps track of your current position. Each read advances the pointer, so once you reach the end of the file, further reads return nothing. The rewind() function moves that pointer back to byte 0 — the very beginning — letting you read the same file again without closing and reopening it.

This page explains what rewind() does, its syntax and return value, when to reach for it, the common pitfalls, and runnable examples you can adapt.

What the file pointer is

Think of the file pointer as a cursor inside the open file. Functions like fread(), fgets(), and fgetcsv() read from that cursor and then move it forward by the number of bytes consumed. You can inspect the cursor with ftell() and move it to an arbitrary position with fseek().

rewind($handle) is the convenient shorthand for "jump back to the start" — it is equivalent to fseek($handle, 0).

Syntax

rewind(resource $handle): bool
  • $handle — a file pointer resource that is still open, typically returned by fopen(). It must point to a stream that can be repositioned.
  • Returns true on success and false on failure.

Why and when to use it

Use rewind() whenever you need to read or process the same open file more than once in a single script run:

  • Counting then processing. First pass to count lines or validate, second pass to do the real work.
  • Re-reading after writing. After writing to a file opened in a read/write mode ('w+', 'r+', 'a+'), rewind before reading back what you wrote.
  • In-memory streams. When you build content in a php://memory or php://temp stream, rewind before reading it out.

If you only ever read a file once, you don't need rewind() at all.

Examples

Example 1: Read the same file twice

This example creates a temporary file, reads it to the end, then rewinds and reads it again.

<?php

// Create a temporary file we can read and write.
$handle = tmpfile();
fwrite($handle, "line one\nline two\n");

// Move to the start so we can read what we just wrote.
rewind($handle);

echo "First read:\n";
echo fread($handle, 1024);

// The pointer is now at the end; reading again gives nothing.
echo "After first read, position: " . ftell($handle) . "\n";

// Rewind and read the whole file again.
rewind($handle);
echo "Second read:\n";
echo fread($handle, 1024);

fclose($handle);

Output:

First read:
line one
line two
After first read, position: 18
Second read:
line one
line two

The first fread() leaves the pointer at byte 18 (the end of the file). Without rewind(), a second read would return an empty string. After rewinding, the pointer is back at 0 and the full content is available again.

Example 2: Write, rewind, then read back

When a stream is opened for both writing and reading, rewind() lets you verify what you wrote.

<?php

// php://memory is an in-memory read/write stream.
$handle = fopen('php://memory', 'r+');

fwrite($handle, 'Hello, W3Docs!');

// Without rewind, the pointer sits after the written text,
// so reading now would return an empty string.
rewind($handle);

$content = stream_get_contents($handle);
echo $content; // Hello, W3Docs!

fclose($handle);

Output:

Hello, W3Docs!

Example 3: Always check the return value

rewind() returns false when the stream cannot be repositioned (for example, a non-seekable network or pipe stream).

<?php

$handle = fopen('php://memory', 'r+');
fwrite($handle, 'data');

if (rewind($handle)) {
    echo "Pointer reset. Position: " . ftell($handle) . "\n";
} else {
    echo "This stream cannot be rewound.\n";
}

fclose($handle);

Output:

Pointer reset. Position: 0

Common pitfalls

  • The pointer is already at the end after reading. This is the whole reason rewind() exists. If a second read returns nothing, you likely forgot to rewind.
  • Append mode ('a' / 'a+'). Rewinding moves the read pointer to the start, but in append mode every fwrite() still appends to the end of the file regardless of the pointer position.
  • Non-seekable streams. Pipes, sockets, and some HTTP streams cannot be rewound; rewind() returns false. Check the return value when the source might not be seekable.
  • Closed handles. Calling rewind() on a handle already passed to fclose() raises a warning. Rewind before you close.
  • fseek() — move the pointer to any byte offset (rewind() is fseek($handle, 0)).
  • ftell() — report the current pointer position.
  • fopen() — open a file or stream and get the handle.
  • fread() / fgets() — read from the current position.
  • fclose() — close the handle when you are done.

Conclusion

rewind() resets an open file's internal pointer to the beginning so you can re-read or re-process the data in the same script run. It is a thin, convenient wrapper around fseek($handle, 0). Remember to check its return value for non-seekable streams, keep append-mode behavior in mind, and rewind before closing the handle.

Practice

Practice
What is the function of the 'rewind()' function in PHP?
What is the function of the 'rewind()' function in PHP?
Was this page helpful?