2-Create and modify file content
file_put_contentsWrite files
Let’s first learn the first way to write files:
int file_put_contents (string $file Path, string $write data])
Function: Write a string to the specified file. If the file does not exist, create the file. What is returned is the length of written bytes
We found that writing files is quite simple. According to the format of this function, specify the file and write the string data.
fwrite cooperates with fopen to perform writing operations
int fwrite (resource $file resource variable, string $written string[, int Length])
Note: The alias function of fwrite is fputs
We tried r mode in the last class, and it was only used when reading. Next, we use fwrite plus fopen. w, write mode to write files.
Let’s take a look at the features:
Open in writing mode, point the file pointer to the file header and cut the file size to zero. If the file does not exist then attempts to create it.
Note: In the following experiment, you can try to create a new test.txt file and write content into it. Then, you can try to delete test.txt. See what tips there are.
Summary:
1. Regardless of whether there is a new file, the file will be opened and rewritten
2. The original file content will be overwritten
3. If the file does not exist, it will be created
Let’s compare the differences between the following modes:
: If you are confused in college, PHP gives you hope.