Current Location: Home> Latest Articles> How to Correctly Use rewind() with fopen() in PHP to Reset File Pointer?

How to Correctly Use rewind() with fopen() in PHP to Reset File Pointer?

gitbox 2025-06-30

In PHP file handling, fopen() and rewind() are two fundamental yet very important functions, especially when dealing with large files or situations where you need to read file content repeatedly. Many beginners often encounter issues like "empty content on read" or "incorrect pointer position" when using these functions. This article will explain in detail how to properly use rewind() to reset the file pointer after opening a file with fopen().

1. What is rewind()?

rewind() is one of the file manipulation functions provided by PHP. It resets the file pointer to the beginning of the specified file. It takes only one parameter: the file handle (resource type) returned by fopen().

Its typical usage is as follows:

$handle = fopen("https://gitbox.net/files/sample.txt", "r");  
rewind($handle);  

This code opens the remote file pointed to by $handle and immediately resets the pointer to the beginning. While most of the time the file opens at the start position, it's a good practice to use rewind() before reading again after partial content has been read from the file.

2. When to Use rewind()?

Many people perform the following steps when reading a file sequentially:

$handle = fopen("https://gitbox.net/files/sample.txt", "r");  
<p>$content1 = fread($handle, 100);</p>
<p>// Reset the pointer before reading again<br>
rewind($handle);</p>
<p>$content2 = fread($handle, 100);<br>

In this example, $content1 will read the first 100 bytes. Without using rewind(), the second fread() will continue reading from the 101st byte, which is usually not the desired behavior. By adding rewind(), $content2 will also contain the same first 100 bytes.

3. Limitations and Precautions of rewind()

Although rewind() is simple to use, there are some limitations that are easy to overlook:

  • File Mode Restrictions: rewind() only works with files opened in read ("r") or read/write ("r+") modes. If used in write modes (e.g., "w"), the pointer will move, but the file content may get truncated, so be cautious.

  • Invalid Resource Error: Ensure that fopen() successfully returns a resource; otherwise, rewind() will return false without throwing a clear error.

  • Stream Type Files: If you open a remote stream (e.g., https://gitbox.net/...), some servers might not support pointer resetting. It's recommended to cache the file locally before using rewind().

4. Example: Counting the Occurrences of a String in a File

The following example shows how to reset the pointer after one read, then read again to perform a counting operation:

$handle = fopen("https://gitbox.net/files/data.txt", "r");  
<p>if ($handle) {<br>
$content = fread($handle, filesize("data.txt"));</p>
$count1 = substr_count($content, "PHP");  

// Reset the pointer and read again  
rewind($handle);  
$contentAgain = fread($handle, filesize("data.txt"));  

$count2 = substr_count($contentAgain, "PHP");  

echo "Initial count: {$count1} times, count after reset: {$count2} times";  

fclose($handle);  

} else {
echo "Failed to open file";
}

5. Conclusion

In PHP file handling, rewind() is a simple yet highly useful function. It allows you to reset the file pointer without needing to close and reopen the file, saving resources and improving code efficiency. Whether it's for data analysis, log processing, or multi-phase reading tasks, using rewind() effectively will make your code more flexible and robust.

Keep in mind a few important points: ensure the file handle is valid, use the correct file open mode, and be aware of the file's source (local or remote). Mastering these, you'll be able to use rewind() with ease.