- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP rewind(): Move File Pointer to Beginning of File
The PHP rewind() function is used when we need to rewind the file pointer to the beginning of the file. For example:
<?php $fp = fopen("myfile.txt", "w"); fwrite($fp, "PHP is Fun!"); echo "<p>The file pointer position: " .ftell($fp). "</p>"; rewind($fp); echo "<p>Now the file pointer position: " .ftell($fp). "</p>"; fclose($fp); ?>
The snapshot given below shows the output produced by the above PHP example:
Let me create another example of the rewind() function in PHP:
<?php $file = "myfile.txt"; $fp = fopen($file, "w+"); if($fp) { fwrite($fp, "PHP is Fun!"); rewind($fp); $content = fread($fp, filesize($file)); echo $content; fclose($fp); } else echo "<p>Unable to open the file</p>"; ?>
The output of the above PHP example using the rewind() function is shown in the snapshot given below:
That is, after writing the text PHP is Fun! to the file named myfile.txt using the function fwrite(). The file pointer ($fp) goes to the end of the file. Therefore, with the help of the rewind() function, the file pointer gets moved to the beginning of the file, and using the fread() function, the content gets read and initialized to the $content variable. Finally, the value of the $content variable gets printed on the output, using the echo statement/keyword.
PHP rewind() Syntax
The syntax of the rewind() function in PHP is:
rewind(filePointer)
Advantages of the rewind() function in PHP
- The primary benefit of utilizing the "rewind()" function is that it resets the file pointer to the beginning of a file, which enables you to read the contents of the file once more starting from the beginning of the file. This is the primary advantage of utilizing the "rewind()" function.
- In order to carry out more complex file operations, the "rewind()" function can be used in conjunction with other file functions such as "fseek()" and "ftell()."
- The "rewind()" function is an effective way to reset the file pointer and begin reading the file once more. This function is most useful for working with small files.
Disadvantages of the rewind() function in PHP
- Large files may not benefit from the "rewind()" function because of how slow and memory-intensive it can be.
- The "rewind()" function might not function properly with non-seekable streams, such as pipes or network sockets.
- The "rewind()" function is not thread-safe, which means that the outcome may be unpredictable if multiple threads access the same file.
« Previous Tutorial Next Tutorial »