The rewind() function in PHP is an inbuilt function which is used to set the position of the file pointer to the beginning of the file.
Any data written to a file will always be appended if the file is opened in append (“a” or “a+”) mode regardless of the file pointer position.
The file on which the pointer has to be edited is sent as a parameter to the rewind() function and it returns True on success or False on failure.
The rewind() function in PHP accepts one parameter.
- file : It is a mandatory parameter which specifies the file to be edited.
It returns True on success or False on failure.
Errors And Exception:
- The rewind() function generates an E_WARNING level error on failure.
- The stream must be “seekable” for using the rewind() function.
- If the file is opened in append mode, the data written will be appended regardless of the position of the pointer.
Input: $myfile = fopen("gfg.txt", "r"); fseek($myfile, "10"); rewind($myfile); fclose($file); Output: 1 Input : $myfile = fopen("gfg.txt", "r+"); fwrite($myfile, 'geeksforgeeks'); rewind($myfile); fwrite($myfile, 'portal'); rewind($myfile); echo fread($myfile, filesize("gfg.txt")); fclose($myfile); Output : portalforgeeks Here all characters of the file as it is after rewind "portal"
Below are the programs to illustrate the rewind() function.
geeksportalks a computer science portal
- PHP | SplFixedArray rewind() Function
- PHP | SplFileObject rewind() Function
- PHP | ArrayIterator rewind() Function
- PHP | SplDoublyLinkedList rewind() Function
- PHP | SimpleXMLIterator rewind() Function
- PHP | SplObjectStorage rewind() Function
- How to get the function name inside a function in PHP ?
- PHP | pow( ) Function
- PHP | pos() Function
- PHP | key() Function
- D3.js | d3.set.add() Function
- p5.js | log() function
- p5.js | cos() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.