PHP to read and write file implementation code. To read and write files in PHP, you can use the following built-in functions: 1. fopen (create file and open file) syntax: Copy the code as follows: fopen (filename, mode) filename, requires to open the file read and write in PHP, you can use the built-in function:
1. fopen (create a file and open a file)
Syntax:
The code is as follows:
Fopen (filename, mode)
Filename, specifies the file to be opened. Mode. the possible values are shown in the following table.
Mode description
"R" is opened in read-only mode, pointing the file pointer to the beginning of the file.
Open the "r +" read/write mode and point the file pointer to the beginning of the file.
Open the "w" write mode, point the file pointer to the beginning of the file, and cut the file size to zero. If the file does not exist, try to create it.
Open in "w +" read/write mode. point the file pointer to the beginning of the file and cut the file size to zero. If the file does not exist, try to create it.
"A" is opened in writing mode, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
Open the "a +" read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
If the file is successfully opened, the fopen function returns a file pointer. If an error occurs, FALSE is returned.
Example:
The code is as follows:
$ Fp = fopen ("test.txt", "r ");
?>
2. fclose (close the file)
Syntax:
Fclose (filepointer)
Filepointer: the object pointer to be closed. If the operation succeeds, the fclose function returns TRUE. if the operation fails, the fclose function returns FALSE.
Example:
The code is as follows:
$ Fp = fopen ("test.txt", "r ");
Fclose ($ fp );
?>
3. feof (check whether it has reached the end of the file)
Syntax:
Feof (filepointer)
Filepointer: The file pointer to be checked. the pointer must point to a file that is successfully opened but not closed. If the file pointer reaches the end of the file or an error occurs, the feof function returns TRUE.
Example:
The code is as follows:
$ Fp = fopen ("test.txt", "r ");
While (! Feof ($ fp ))
{
Echo fgets ($ fp )."
";
}
Fclose ($ fp );
?>
4. fgets (read a row from the file pointer)
Syntax:
Fgets (filepointer)
Filepointer: the object pointer to be read. If the operation succeeds, a row is read from the file and a string is returned. if the operation fails, FALSE is returned.
Example:
The code is as follows:
$ Fp = fopen ("test.txt", "r ");
If ($ fp)
{
For ($ I = 1 ;! Feof ($ fp); $ I ++)
{
Echo "line". $ I. ":". fgets ($ fp )."
";
}
}
Else
{
Echo "failed to open the file ";
}
Fclose ($ fp );
?>
Assume that the content of test.txt is:
Hello world
Hello cnblogs
Hello heihaozi
Hello everyone
The output result of the page is:
Row 1: hello world
Row 2: hello cnblogs
Row 3: hello heihaozi
Row 4: hello everyone
5. fwrite (Write file)
Syntax:
Fwrite (filepointer, string)
Filepointer: The file pointer to be written. String, the string to be written. If the write operation succeeds, the number of written characters is returned. if the operation fails, FALSE is returned.
Example:
The code is as follows:
$ Fp = fopen ("test.txt", "w"); // The file is cleared before being written.
If ($ fp)
{
$ Count = 0;
For ($ I = 1; $ I <= 5; $ I ++)
{
$ Flag = fwrite ($ fp, "line". $ I. ":". "Hello World! \ R \ n ");
If (! $ Flag)
{
Echo "failed to write file
";
Break;
}
$ Count + = $ flag;
}
Echo "a total of". $ count. "characters ";
}
Else
{
Echo "failed to open the file ";
}
Fclose ($ fp );
?>
The output result of the page is:
Write 100 characters in total
The test.txt file will be written:
Row 1: Hello World!
Row 2: Hello World!
Row 3: Hello World!
Row 4: Hello World!
Row 5: Hello World!
Note: to simplify the operation, some function optional parameters are not listed.
Syntax 1. fopen (create a file and open a file): Code: fopen (filename, mode) filename, required to open...