• PHP Video Tutorials

PHP - Function rewind()



The rewind() function can rewind the position of file pointer to the beginning of the file, and it can return true on success, or false on failure.

Syntax

bool rewind ( resource $handle )

This function can set the file position indicator for a handle to the beginning of the file stream. If we have opened a file in append ("a" or "a+") mode, any data we write to a file can always be appended, regardless of the file pointer position.

Example-1

<?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

Hello PHPence

Example-2

<?php
   $file = fopen("/PhpProject/sample.txt", "r");

   fseek($file, "15");  // Change the position of file pointer
   rewind($file);  // Set the file pointer to 0
   
   fclose($file);
?>
php_function_reference.htm
Advertisements