The best PHP File Tutorial In 2024, In this tutorial you can learn open a file,Close the file,End detecting file (EOF),Read the file line by line,Read a file character by character,PHP Filesystem Reference Manual,
fopen () function is used to open files in PHP.
fopen () function is used to open files in PHP.
The first parameter of this function contains the name of the file you want to open, and the second parameter specifies which mode to use to open the file:
File may be opened by the following modes:
模式 | 描述 |
---|---|
r | 只读。在文件的开头开始。 |
r+ | 读/写。在文件的开头开始。 |
w | 只写。打开并清空文件的内容;如果文件不存在,则创建新文件。 |
w+ | 读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。 |
a | 追加。打开并向文件末尾进行写操作,如果文件不存在,则创建新文件。 |
a+ | 读/追加。通过向文件末尾写内容,来保持文件内容。 |
x | 只写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。 |
x+ | 读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。 |
Note: If thefopen () function does not open the specified file, it returns 0 (false).
If the fopen () function does not open the specified file, the following example will generate some news:
fclose () function is used to close an open file:
Are feof () function detects the end of file has been reached (EOF).
When looping through data of unknown length, feof () function is useful.
NOTE: In w, a, and x mode, you can not open the file read!
fgets () function is used to read the file line by line from the file.
Note: After a call to this function the file pointer moves to the next line.
The following example reads the file line by line, until the end of the file so far:
fgetc () function is used to read a file character by character from a file.
Note: After a call to this function the file pointer moves to the next character.
The following example reads a file character by character, until the end of the file so far:
For a complete reference manual PHP filesystem functions, visit our PHP Filesystem Reference Manual .