Home  >  Article  >  Backend Development  >  PHP function introduction—rewind(): Reposition the file pointer to the beginning of the file

PHP function introduction—rewind(): Reposition the file pointer to the beginning of the file

WBOY
WBOYOriginal
2023-07-24 16:01:521811browse

PHP function introduction—rewind(): Reposition the file pointer to the beginning of the file

In PHP, we often need to handle file operations. PHP provides a wealth of functions to operate files, one of which is the rewind() function. rewind()The function is used to relocate the file pointer to the beginning of the file in order to re-read the file content or perform other file operations.

Below we will introduce the use of the rewind() function in detail and provide some code examples to help everyone better understand the function of this function.

Usage:
rewind(resource $handle): bool

Parameter description:

  • $handle: required. Represents a file pointer resource.

Return value:

  • Returns true when successful and false when failed.

Code example 1: Read the file contents and use the rewind() function to relocate the file pointer to the beginning of the file.

$file = fopen("example.txt", "r");

// 读取文件内容
while (!feof($file)) {
    echo fgets($file) . "<br>";
}

// 将文件指针重新定位到文件开头
rewind($file);

// 再次读取文件内容
while (!feof($file)) {
    echo fgets($file) . "<br>";
}

fclose($file);

The above code first opens a text file named example.txt, and reads the file content line by line through the fgets() function and outputs it. Next, use the rewind() function to relocate the file pointer to the beginning of the file. Finally, use the fgets() function again to read the file content and output it. Through this code, we can see that the file content is read twice and successfully positioned at the beginning of the file.

Code example 2: Use the rewind() function to perform file operations.

$file = fopen("example.txt", "w+");

// 写入文件内容
fwrite($file, "Hello, PHP!");

// 将文件指针重新定位到文件开头
rewind($file);

// 读取文件内容
echo fgets($file);

fclose($file);

The above code first uses the fopen() function to create a new file example.txt. The file opening mode is "w", which means it can be read and written. Next, use the fwrite() function to write the string "Hello, PHP!" to the file. Then, use the rewind() function to relocate the file pointer to the beginning of the file. Finally, use the fgets() function to read the file content and output it. Through this code, we can see that the written file content is successfully output.

Summary:
Through the rewind() function, we can easily relocate the file pointer to the beginning of the file in order to re-read the file content or perform other file operations. Whether it is reading file content or writing file content, the rewind() function can help us perform better file operations.

Before using the rewind() function, you need to ensure that the file has been opened, and after the function is successfully executed, the file pointer will be relocated to the beginning of the file. It should be noted that when using the rewind() function, it needs to be combined with other file operation functions to complete specific file operation requirements.

Hope this article can help you understand and correctly use the rewind() function. Good luck with your file operations!

The above is the detailed content of PHP function introduction—rewind(): Reposition the file pointer to the beginning of the file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn