the fopen function is used to open the file in PHP.
The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file;
The file may be opened in the following mode:
-------------------------------------------------------------------------------------------
mode | Describe
-------------------------------------------------------------------------------------------
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 empty the contents of the file, or create a new file if the file does not exist.
-------------------------------------------------------------------------------------------
w+ | Read/write. Open and empty the contents of the file, or create a new file if the file does not exist.
-------------------------------------------------------------------------------------------
A | Additional. Open and write to the end of the file file, or create a new file if the file does not exist
------------------------------------------------------------------------------ ------------
A + | Read/Append. Keep the contents of the file by writing to the end of the file.
-------------------------------------------------------------------------------------------
x | Write only. Creates a new file. Returns FALSE if the file already exists.
-------------------------------------------------------------------------------------------
x+ | Read/write. Creates a new file. If the file already exists, it returns FALSE and an error.
-------------------------------------------------------------------------------------------
Note: if fopen () cannot open the specified file, 0 (FALSE) is returned.
-------------------------------------------------------------------------------------------
If fopen () cannot open the specified file, the following example generates a message:
<?php
$file = fopen ("Lala.txt", "R") or exit ("Unable to open the file");
?>
-------------------------------------------------------------------------------------------
To close a file:
The fclose () function is used to close open files.
<?php
$file = fopen ("Test.txt", "R");
Some code to be executed
Fclose ($file);
?>
-------------------------------------------------------------------------------------------
Detection End-of-file
the feof () function detects if the end of the file (EOF) has been reached.
the feof () function is useful when iterating through data of unknown length.
Note: In W, a, and x mode, you cannot read open files!
-------------------------------------------------------------------------------------------
read a file line-wise
The fgets () function is used to read a file from file to line.
Note: After the function is called, the file pointer moves to the next line.
<?php
$file = fopen ("Kecily.txt", "R") or exit ("Unable to open the file");
while (!feof ($file)) {
Echo fgets ($file). " <br> ";
}
Fclose ($file);
?>
-------------------------------------------------------------------------------------------
Read files by character literal
The fgetc () function is used to read files verbatim from a file.
Note: After the function is called, the file pointer moves to the next character.
<?php
$file = fopen ("Kecily.txt", "R") or exit ("Unable to open the file");
while (!feof ($file)) {
echo fgetc ($file).;
}
echo "<br>";
Fclose ($file);
?>
PHP File Processing ~ ~ Learning Notes