Phpfile Processing
The fopen () function is used to open the file in PHP.
Open File
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:
PHP $file=fopen("Welcome.txt", "R");? ></body>
The file may be opened in the following mode:
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 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. Opens and writes to the end of the file, creating a new file if the file does not exist. |
A + |
Read/Append. Preserves the contents of the file by writing to the end of the file. |
X |
Write only. Creates a new file. If the file already exists, it returns FALSE and an error. |
x+ |
Read/write. Creates a new file. If the file already exists, it returns FALSE and an error. |
Note: if the fopen () function cannot open the specified file, 0 (FALSE) is returned.
Instance
If the fopen () function cannot open the specified file, the following instance generates a message:
PHP $file=fopenexit("Unable to open file!" );? ></body>
Close FileThe fclose () function is used to close open files:
<?php
$file = fopen ("Test.txt", "R");
Execute some code
Fclose ($file);
?>
Detect end of file (EOF)The feof () function detects if the end of file (EOF) has been reached.
The feof () function is useful when iterating through data of unknown length.
Note: You cannot read open files in W, a, and X modes!
if (feof ($file)) echo "End of File";
Read a file line-wiseThe 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.
InstanceThe following instance reads the file line-by-row until the end of the file:
<? PHP $file fopen Exit ("Cannot open file!") ); // read each line of the file until the end of the file while (! feof ($file)) { echofgets($file). "<br>";} fclose ($file);? >
Read files by character literalThe 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.
InstanceThe following instance reads the file character by word, until the end of the file:
<? PHP $file=fopenexit("Cannot Open file!" ); while (! feof ($file)) { echofgetc($file);} fclose ($file);? >
PHP Filesystem Reference ManualTo view a complete reference manual for PHP file system functions, please visit our PHP Filesystem reference manual.
PHP include and requirePHP File UploadList of Notes
Vladimir
137***[email protected]
PHP handles the comma delimiter file (*.csv).
You can use a CSV file (a type of text file) to store data more conveniently if you need to deal with less data.
For example, in the same directory of PHP code has a a.csv file, the content is as follows: (note comma is half-width English)
Xiao Wang, Xiao Hong, Xiao Ming, Xiao Van
How does the PHP code:
<? PHP $fh=fopen("A.csv", "R"); // Here we just read the data, so we use read-only to open the file stream $arr=fgetcsv($fh); // This function is to read the CSV file function, he read the text into the array stored in the $arr fcloase ($fh); foreach ($arras$key=$value) {echo$value;} // loop out all the values ?>
Note: The CSV text encoding must be the same as the HTML encoding, otherwise in PHP written to the HTML, users will see garbled. Transcoding can also be done using the Iconv transcoding function.
PHP Advanced Tutorials-Files