Home  >  Article  >  Backend Development  >  How to modify txt file in php

How to modify txt file in php

藏色散人
藏色散人Original
2020-07-31 10:00:214414browse

How to modify txt files in php: first use fopen to open the local txt file; then use fread to read the contents of the file; then modify and replace the text content; finally use fwrite to rewrite the modified file. Just replace and write it into a txt file.

How to modify txt file in php

Recommended: "PHP Video Tutorial"

php to modify the txt file:

1. Use fopen to open the local txt file.

fopen() function opens a file or URL. If fopen() fails, it returns FALSE with an error message. You can hide error output by adding an '@' in front of the function name.

2. Use fread to read the contents of the file.

fread() function reads an open file. The function will stop running when it reaches the specified length or reaches the end of file (EOF), whichever comes first.

This function returns the read string, or FALSE if it fails.

3. Modify and replace the text content.

4. Use fwrite to replace the modified file and write it into the txt file.

fwrite() function writes the contents to an open file.

The function will stop running when it reaches the specified length or reaches the end of file (EOF) (whichever comes first).

If the function executes successfully, the number of bytes written is returned. On failure, returns FALSE.

The above is the detailed content of How to modify txt file in php. 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