File Operation steps: open, operate, and close. $ Fopenfopen (path, method), fwrite ($ fopen, Written string), and fclose ($ fopen ). The Mode description is r-read-only. Start at the beginning of the file. R + read/write. Start at the beginning of the file. W write only. Open and clear the file content.
File Operation steps: open, operate, and close. $ Fopen = fopen (path, method), fwrite ($ fopen, Written string), and fclose ($ fopen ). The Mode description is r-read-only. Start at the beginning of the file. R + read/write. Start at the beginning of the file. W write only. Open and clear the file content.
File Operation steps: open, operate, and close. $ Fopen = fopen (path, method), fwrite ($ fopen, Written string), and fclose ($ fopen ).
The following methods can be used to open a file:
| Mode |
Description |
| R |
Read-only. Start at the beginning of the file. |
| R + |
Read/write. Start at the beginning of the file. |
| W |
Write-only. Open and clear the file content. If the file does not exist, create a new file. |
| W + |
Read/write. Open and clear the file content. If the file does not exist, create a new file. |
| A |
Append. Open and write to the end of the file. If the file does not exist, create a new file. |
| A + |
Read/append. Write content to the end of the file to keep the file content. |
| X |
Write-only. Create a new file. If the file already exists, FALSE is returned. |
| X + |
Read/write. Create a new file. If the file already exists, FALSE and an error are returned. NOTE: If fopen () cannot open the specified file, 0 (false) is returned ). |
The following describes how to read a file:
1. open the file to read
1. readfile ()
Readfile ("file1.txt ");
2. file ()
$ F_arr = file ("file1.txt ");
Foreach ($ f_arr as $ content)
{
Echo $ content ."
";
}
3. file_get_contents ()
$ F_chr = file_get_contents ("file1.txt ");
Echo $ f_chr;
Ii. open the file to read
1. fgets ()
$ Fopen = fopen ("file1.txt", "rb ");
While (! Feof ($ fopen) // function test whether the pointer has reached the end of the file
{
Echo fgets ($ fopen); // output the current row
}
Fclose ($ fopen );
2. fgetss ()
$ Fopen = fopen ("file.txt", "rb ");
While (! Feof ($ fopen) // function test whether the pointer has reached the end of the file
{
Echo fgetss ($ fopen); // output the current row
}
Fclose ($ fopen );
3. fgetc ()
$ Fopen = fopen ("file1.txt", "rb"); // open the file stream
While (false! ==( $ Chr = fgetc ($ fopen) // gets a string and determines whether it is false.
{// If not, output this character
Echo $ chr;
}
Fclose ($ fopen); // close file resources