English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP rewind() Function Usage and Example

PHP Filesystem Reference Manual

The rewind() function can move the file pointer position back to the beginning of the file, and returns true on success and false on failure.

Syntax

bool rewind ( resource $handle )

 Set the file position pointer of the handle to the beginning of the file stream.
 Note: If the file is opened in append mode ("a" or "a+Mode opens, any data written to the file is always appended to the end, regardless of the position of the file pointer.

Example1

<?php
   $handle = fopen("/PhpProject/sample.txt", "r+");
   fwrite($handle, "Long sentence");
   rewind($handle);
   fwrite($handle, "Hello PHP");
   rewind($handle);
 
   echo fread($handle, filesize("/PhpProject/sample.txt"));
   fclose($handle);
?>

Output Result

Hello PHPence

Example2

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   fseek($file, "15");  // Change the position of the file pointer
   rewind($file);  // Set the file pointer to 0
   
   fclose($file);
?>

PHP Filesystem Reference Manual