rewind

Moves a stream's internal file pointer back to the start of the file.

Signature
rewind(resource $stream): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$streamresourceAn open file pointer from fopen(), fsockopen() or similar, not yet closed by fclose(). It must point to a seekable stream.

Return value

boolReturns true on success or false on failure.

Errors

Emits an E_WARNING and returns false if the stream is not seekable or the argument is not a valid open stream.

rewind() puts the read/write position of an open stream back to byte 0. It is exactly equivalent to fseek($stream, 0, SEEK_SET), and like fseek() it also clears the end-of-file flag, so a handle that has been read to exhaustion can be read again from the top.

When to reach for it

  • You wrote to a handle and now want to read back what you wrote on the same handle.
  • You need a second pass over a file you already streamed line by line, without paying for another fopen().
  • You built content in a tmpfile() or php://temp stream and want to hand a readable stream to something else.

What trips people up

Rewinding does not truncate. If you rewind and write a short string over a long one, the tail of the old content stays behind — that is why the classic example turns "Really long sentence." into "Foolly long sentence.". Use ftruncate() when you mean to replace.

Not every stream is seekable. Pipes, sockets, php://output and many wrapper streams return false. Check stream_get_meta_data($stream)['seekable'] first if the source is not a plain local file.

Handles opened in append mode (a, a+) can be rewound for reading, but every write still lands at the end of the file regardless of where the pointer sits.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Write a CSV into a temporary stream, then rewind it and hand it to fgetcsv() for validation before saving.
  • Re-read a config file a second time after a first pass counted its lines.
  • Reset an in-memory php://temp stream so it can be copied to a real file with stream_copy_to_stream().

Frequently asked questions

What is the difference between rewind and fseek?

There is none in effect: rewind($fh) is the same call as fseek($fh, 0, SEEK_SET). rewind() returns a bool, while fseek() returns 0 on success and -1 on failure, so the two report errors differently.

Does rewind delete the contents of the file?

No. It only moves the file pointer to byte 0. To empty the file use ftruncate($fh, 0), or reopen it with the "w" mode which truncates on open.

Why does rewind return false on my stream?

The stream is not seekable — typically a network socket, a pipe from popen(), php://output, or a compressed wrapper. Check stream_get_meta_data($fh)['seekable'] before relying on rewind().

More examples

Read a file twice with one handle

The first loop reads to EOF. Without the rewind, fgets() would keep returning false. After rewinding, the pointer is at 0 and the EOF flag is cleared, so the first line comes back.

Example · php
<?php
file_put_contents('demo.txt', "alpha\nbeta\ngamma\n");
$fh = fopen('demo.txt', 'r');

$lines = 0;
while (fgets($fh) !== false) { $lines++; }
echo "lines: $lines\n";

rewind($fh);            // also clears the EOF flag
echo 'first again: ', fgets($fh);

fclose($fh);
unlink('demo.txt');

Truncate before rewriting

Prints "short" and nothing else. Compare with the main example, where skipping ftruncate() leaves the rest of the old string visible after the new text.

Example · php
<?php
$fh = fopen('demo.txt', 'w+');
fwrite($fh, 'the original longer content');

rewind($fh);
ftruncate($fh, 0);      // now the tail is gone
fwrite($fh, 'short');

rewind($fh);
echo fread($fh, 1024), "\n";

fclose($fh);
unlink('demo.txt');

Check that a stream can be rewound

php://memory reports seekable as true, so rewind() succeeds and the buffered text can be read back. A socket or a pipe would report false and rewind() would fail with a warning.

Example · php
<?php
$mem = fopen('php://memory', 'w+');
fwrite($mem, 'buffered');
$meta = stream_get_meta_data($mem);
var_dump($meta['seekable']);
var_dump(rewind($mem));
echo fread($mem, 64), "\n";
fclose($mem);

Discussion

  • Be the first to comment on this lesson.