W3docs

rewind()

Learn how PHP's rewind() resets a file pointer to the start so you can re-read it. Covers syntax, return value, examples, and fseek() vs rewind().

Introduction

When you open a file in PHP, the system keeps track of a file pointer — an internal position that marks where the next read or write will happen. Every time you read with fgets() or fread(), that pointer moves forward. The rewind() function sends the pointer back to the very beginning of the file (byte 0) so you can read it again from the start without closing and reopening it.

This chapter explains what rewind() does, its syntax and return value, and when you would reach for it instead of fseek().

Syntax

rewind(resource $stream): bool
  • $stream — a file pointer returned by fopen() (or a similar stream resource). The stream must be valid and seekable.
  • Returns true on success and false on failure.

Calling rewind($stream) is equivalent to fseek($stream, 0): both move the pointer to the start of the file. Use ftell() at any time to read the pointer's current position.

Note: On streams opened in append mode ('a' or 'a+'), the read pointer is moved to the beginning, but writes are still appended to the end of the file.

Example: re-reading a file from the start

After reading part of a file, rewind() lets you start over from the top:

<?php

$handle = fopen('notes.txt', 'r');

// Read the first line — this advances the pointer
echo fgets($handle);                       // e.g. "First line"
echo 'Pointer is at: ' . ftell($handle);   // a non-zero byte offset

// Send the pointer back to the beginning
rewind($handle);
echo 'After rewind: ' . ftell($handle);    // 0

// Re-read the same first line
echo fgets($handle);                        // "First line" again

fclose($handle);

The first fgets() reads a line and leaves the pointer partway through the file, so ftell() reports a non-zero offset. After rewind(), ftell() returns 0, and the next read starts again from the first byte.

When to use rewind()

  • Two passes over the same file. For example, count the lines in a file, then loop again to process them — without reopening the file.
  • Reset after writing. You write to a temporary stream, then rewind() and read back what you just wrote.
  • Re-reading a stream you only have a handle to, such as php://memory or php://temp.

Reach for fseek() instead when you need to jump to an arbitrary offset rather than the very beginning. For directory handles, the equivalent reset is rewinddir().

Common pitfalls

  • Non-seekable streams. Some streams (network sockets, certain pipes) cannot be rewound; rewind() returns false. Always check the return value if the stream may not be seekable.
  • Confusing the read and write pointers in append mode. In 'a'/'a+' mode, rewinding does not let you overwrite from the top — new data still goes to the end.
  • Forgetting that reads still advance the pointer. rewind() only resets the position; the very next fgets()/fread() will move it forward again.

Conclusion

rewind() resets a file pointer to the beginning of the file so you can read it again from the start. It returns true on success and false on failure, and behaves like fseek($stream, 0). It pairs naturally with fopen(), fgets(), ftell(), and fseek() when you need to make more than one pass over the same file or stream.

Practice

Practice
What does the PHP function 'rewind()' do?
What does the PHP function 'rewind()' do?
Was this page helpful?