PHP file processing ~~Study Notes
The fopen function is used to open files 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;
Files may be opened in the following modes:
-------------------------------------------------- ----------------------------------------
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. Opens and clears the contents of the file; if the file does not exist, creates a new file.
-------------------------------------------------- ----------------------------------------
w+ | read/write. Opens and clears the contents of the file; if the file does not exist, creates a new file.
-------------------------------------------------- ----------------------------------------
a | Append. Opens and writes to the end of the file, or creates a new file if it does not exist
-------------------------------------------------- ---------------------------- ----------
a+ | read/append. Maintain file contents by writing to the end of the file.
-------------------------------------------------- ----------------------------------------
x | Write only. Create new file. Returns FALSE if the file already exists.
-------------------------------------------------- ----------------------------------------
x+ | read/write. Create new file. If the file already exists, returns FALSE and an error.
-------------------------------------------------- ----------------------------------------
Note: If fopen() cannot open the specified file, it returns 0 (false).
-------------------------------------------------- ----------------------------------------
If fopen() cannot open the specified file, the following example will generate a message:
$file = fopen("lala.txt", "r") or exit("unable to open the file");
?>
-------------------------------------------------- ----------------------------------------
Close file:
The fclose() function is used to close an open file.
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
-------------------------------------------------- ----------------------------------------
Detect End-of-file
The feof() function detects whether the end of file (EOF) has been reached.
The feof() function is useful when looping over data of unknown length.
Note: In w, a and x modes, you cannot read open files!
-------------------------------------------------- ----------------------------------------
Read file line by line
The fgets() function is used to read a file line by line from a file.
Note: After calling this function, the file pointer will move to the next line.
$file = fopen("Kecily.txt", "r") or exit("unable to open the file");
while(!feof($file)) {
echo fgets($file)."
";
}
fclose($file);
?>
-------------------------------------------------- ----------------------------------------
Read file character by character
The fgetc() function is used to read from a file character by character.
Note: After calling this function, the file pointer will move 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);
?>
http://www.bkjia.com/PHPjc/969601.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969601.htmlTechArticlePHP file processing~~Study Notes The fopen function is used to open files 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...