PHP rewind function tutorial shows how to reset file pointers in PHP. Learn rewind with practical examples.
last modified April 3, 2025
The PHP rewind function resets a file pointer to the beginning of a file. It’s essential when you need to read a file multiple times or reset position after operations.
The rewind function sets the file position indicator for a file pointer to the beginning of the file stream. It takes one parameter: the file pointer resource.
Syntax: rewind(resource $stream): bool. The function returns true on success or false on failure. It works with files opened with fopen.
This shows the simplest usage of rewind to reset a file pointer.
basic_rewind.php
<?php
declare(strict_types=1);
$file = fopen(’example.txt’, ‘r’); fread($file, 10); // Read first 10 bytes rewind($file); // Reset to beginning $content = fread($file, filesize(’example.txt’));
echo $content; fclose($file);
This reads 10 bytes, rewinds to start, then reads the entire file. Without rewind, the second read would start from byte 10. Always close files after use.
Rewind is useful after writing to read the written content immediately.
rewind_after_write.php
<?php
declare(strict_types=1);
$file = fopen(’temp.txt’, ‘w+’); fwrite($file, “Hello World”); rewind($file); $content = fread($file, filesize(’temp.txt’));
echo $content; // Outputs: Hello World fclose($file); unlink(’temp.txt’);
We write to a file, rewind to start, then read what we wrote. The ‘w+’ mode allows both read and write operations. Temporary files should be cleaned up.
Rewind helps when processing CSV files multiple times without reopening.
rewind_csv.php
<?php
declare(strict_types=1);
$file = fopen(‘data.csv’, ‘r’);
// First pass: count lines $lineCount = 0; while (!feof($file)) { fgetcsv($file); $lineCount++; } rewind($file);
// Second pass: process data while (($data = fgetcsv($file)) !== false) { print_r($data); }
fclose($file);
This counts lines, rewinds, then processes the CSV data. Without rewind, the second loop wouldn’t run as the pointer would be at EOF. CSV processing often requires multiple passes.
Rewind enables efficient processing of large files in chunks.
large_file.php
<?php
declare(strict_types=1);
$file = fopen(’large.log’, ‘r’); $chunkSize = 1024; // 1KB chunks
// Process first chunk $chunk1 = fread($file, $chunkSize); processChunk($chunk1);
rewind($file);
// Process first chunk again differently $chunk1a = fread($file, $chunkSize); processChunkDifferently($chunk1a);
fclose($file);
function processChunk(string $data): void { echo “Processing chunk: " . strlen($data) . " bytes\n”; }
function processChunkDifferently(string $data): void { echo “Alternative processing: " . strlen($data) . " bytes\n”; }
This demonstrates processing the same chunk multiple ways without reopening. Rewind is more efficient than closing/reopening for large files. Chunk processing conserves memory with big files.
Rewind works with custom stream wrappers that implement seek functionality.
stream_wrapper.php
<?php
declare(strict_types=1);
class MemoryStream { private $position = 0; private $data = ‘’;
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool {
return true;
}
public function stream_read(int $count): string|false {
$ret = substr($this->data, $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
public function stream_write(string $data): int {
$this->data .= $data;
return strlen($data);
}
public function stream_tell(): int {
return $this->position;
}
public function stream_eof(): bool {
return $this->position >= strlen($this->data);
}
public function stream_seek(int $offset, int $whence): bool {
switch ($whence) {
case SEEK_SET:
$this->position = $offset;
return true;
case SEEK_CUR:
$this->position += $offset;
return true;
case SEEK_END:
$this->position = strlen($this->data) + $offset;
return true;
default:
return false;
}
}
}
stream_wrapper_register(‘memory’, ‘MemoryStream’);
$file = fopen(‘memory://test’, ‘w+’); fwrite($file, “Test data”); rewind($file); echo fread($file, 4); // Outputs: Test fclose($file);
This custom stream wrapper demonstrates rewind implementation. The stream_seek method enables rewind functionality. Custom wrappers must implement seek to support rewind.
Error Handling: Check rewind return value for success.
Resource Management: Always close files after use.
Position Awareness: Know your file pointer position.
Mode Compatibility: Ensure file mode supports seeking.
This tutorial covered the PHP rewind function with practical examples showing its usage in different file handling scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.