What is the rewind() function in PHP?
The rewind() function in PHP is a built-in function used to rewind the position of the file pointer. This means that the position of the file pointer is set to the start of the file.
Syntax
rewind(resource $stream): bool
Parameters
stream: A file stream that points to an already opened file.
If a file is opened in “append” mode, i.e.,
"a"or"a+", the position of the pointer continues from where the file was last written, regardless of the position set.
Return value
rewind() returns true if it is successfully executed. Otherwise, in case of any error, it returns false.
Code
The following code shows how the rewind() function works.
main.php
q1.txt
<?php$temp = fopen("q1.txt","r+");// display the contents of the fileecho readfile("q1.txt");//reset the file pointerrewind($temp);//add content at the start of the file, overwriting itfwrite($temp, "\nHello world! ");//read the file againecho readfile("q1.txt");fclose($temp);?>
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved