Php file processing ~~ Study notes. Php file processing ~~ The fopen function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened. The second parameter specifies the mode used to open the php file for processing ~~ Study Notes
The fopen function is used to open a file in PHP.
The first parameter of this function contains the name of the file to be opened, and the second parameter specifies the mode used to open the file;
The file may be opened in the following mode:
Bytes -------------------------------------------------------------------------------------------
Mode | description
Bytes -------------------------------------------------------------------------------------------
R | read-only. Start at the beginning of the file.
Bytes -------------------------------------------------------------------------------------------
R + | read/write. Start at the beginning of the file.
Bytes -------------------------------------------------------------------------------------------
W | write only. Open and clear the file content. if the file does not exist, create a new file.
Bytes -------------------------------------------------------------------------------------------
W + | read/write. Open and clear the file content. if the file does not exist, create a new file.
Bytes -------------------------------------------------------------------------------------------
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.
Bytes -------------------------------------------------------------------------------------------
X | write-only. Create a new file. If the file already exists, FALSE is returned.
Bytes -------------------------------------------------------------------------------------------
X + | read/write. Create a new file. If the file already exists, FALSE and an error are returned.
Bytes -------------------------------------------------------------------------------------------
Note: If fopen () cannot open the specified file, 0 (false) is returned ).
Bytes -------------------------------------------------------------------------------------------
If fopen () cannot open the specified file, the following example generates a message:
$ File = fopen ("lala.txt", "r") or exit ("unable to open the file ");
?>
Bytes -------------------------------------------------------------------------------------------
Close the file:
The fclose () function is used to close open files.
$ File = fopen ("test.txt", "r ");
// Some code to be executed
Fclose ($ file );
?>
Bytes -------------------------------------------------------------------------------------------
Detect End-of-file
The feof () function checks whether it has reached the end of the file (EOF ).
The feof () function is useful when data with unknown length is traversed cyclically.
Note: in w, a, and x modes, you cannot read open files!
Bytes -------------------------------------------------------------------------------------------
Read files row by row
The fgets () function is used to read objects row by row from objects.
Note: after the function is called, the file pointer is moved to the next row.
$ File = fopen ("Kecily.txt", "r") or exit ("unable to open the file ");
While (! Feof ($ file )){
Echo fgets ($ file )."
";
}
Fclose ($ file );
?>
Bytes -------------------------------------------------------------------------------------------
Read files by character
The fgetc () function is used to read files from a file character by character.
Note: after the function is called, the file pointer is moved to the next character.
$ File = fopen ("Kecily.txt", "r") or exit ("unable to open the file ");
While (! Feof ($ file )){
Echo fgetc ($ file ).;
}
Echo"
";
Fclose ($ file );
?>
Region ~~ The fopen function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened. The second parameter specifies the mode used to open the file...