Php learning notes-advanced tutorials-reading files, creating files, and writing files
Open the file: fopen (filename, mode); // fopen ("test.txt", "r"); open mode: r read-only mode, point the file pointer to the file header r + read/write mode to open, point the file pointer to the file header w write mode, point to the file header, if not, try to create w + read/write mode, point to the file header, if it does not exist, try to create a write mode to open the file, point to the end of the file, if it does not exist, try to create a + read/write mode to open, point to the end of the file, if it does not exist, try to create Read File: fread: fread ();
Readfile (filename): reads the file content and writes it to the output buffer.
Fopen (filename, mode): open a file and create a file
Fopen is also used to create files. If a file does not exist, this function will create a file. The Jiading file will be opened to write (w) or add ().
Mode |
Description |
R |
Open the file as read-only. The file pointer starts at the beginning of the file. |
W |
Open the file to write-only. Delete the file content or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
A |
Open the file to write-only. The existing data in the file is retained. The file pointer starts at the end of the file. Create a new file if the file does not exist. |
X |
Create a new file to write-only. Returns FALSE and error if the file already exists. |
R + |
The file is read/write, and the file pointer starts at the beginning of the file. |
W + |
Open the file as read/write. Delete the file content or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
A + |
Open the file as read/write. The existing data in the file is retained. The file pointer starts at the end of the file. Create a new file if it does not exist. |
X + |
Create a new file as read/write. Returns FALSE and error if the file already exists. |
Fwrite (resource, content) // Write File Content
Fread (): The function reads open files.
fread($myfile,filesize("webdictionary.txt"));
Fget (resource, length) // get the file content. If the length is 10, 9 bits can be obtained. fgets (): used to read a single row, fgets (file, lenght ), lenght (optional) specifies the number of bytes to be read. The default value is 1024 bytes.
SlaveFileRead a row from the object to which the returned length is upLength-1-byte string. Line breaks (including returned values), EOF, or readLength-Stop after 1 byte (see the situation first ). If noLengthThe default value is 1 K, or 1024 bytes.
If it fails, false is returned.
Fgetc (): read a single character
Fclose (Resource) // close an open file resource unlink (File) // delete a file
Feof (): Check whether it has reached the end. It is useful for Traversing data with unknown length.
";}Fclose ($ myfile) ;?>
Filesize (filename): filename is the file name, which is a string type. The results of this function will be cached. Clear and try clearstatcache () to clear the cache. Filetype ();