Open File: Fopen:fopen (Filename,mode);//fopen ("Test.txt", "R");Open mode: R read-only open, the file pointer to the file header r+ read-write mode open, the file pointer to the file header W write method, point to the file header, if not present, try to create w+ read The Write method, pointing to the file header, if it does not exist, attempts to create a write mode open, points to the end of the file, if it does not exist, tries to create a + read-write mode open, points to the end of the file, and attempts to create the read file if it does not exist D ();
ReadFile (filename): reads the contents of the file and writes it to the output buffer
<?phpecho ReadFile ("Webdictionary.txt");? >
fopen (Filename,mode): Open file, create file
<?php$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); Echo fread ($myfile, FileSize ("Webdictionary.txt")); fclose ($myfile);? >
Fopen is also used to create files, if a file is opened that does not exist, this function creates a file that the Jiading file is opened for writing (W) or added (a).
Mode |
Description |
R |
Open the file as read-only. The file pointer starts at the beginning of the file. |
W |
Open the file as write-only. Delete the contents of the file 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 write-only. The existing data in the file is retained. The file pointer starts at the end of the file. Creates a new file if the file does not exist. |
X |
Create a new file for write-only. Returns FALSE and an error if the file already exists. |
r+ |
Open the file as read/write, and the file pointer starts at the beginning of the file. |
w+ |
Open the file for read/write. Delete the contents of the file or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
A + |
Open the file for read/write. The data already in the file will be retained. The file pointer starts at the end of the file. Creates a new file if it does not exist. |
x+ |
Create a new file for read/write. Returns FALSE and an error if the file already exists. |
Fwrite (resources, content) Write file contents
<?php$myfile = fopen ("Newfile.txt", "w") or Die ("Unable to open file!"); $txt = "Bill gates\n"; fwrite ($myfile, $txt); $txt = "Steve jobs\n"; fwrite ($myfile, $txt); fclose ($myfile);? >
Fread (): The function reads the open file.
Fread ($myfile, FileSize ("Webdictionary.txt"));
Fget (resource, length) Get the contents of the file,If the length is 10, you can get 9-bitFgets (): Used to read a single line,fgets (file,lenght), lenght optional, specifies the number of bytes to read. The default is 1024 bytes.
Reads a row from the file pointed to by the files and returns a string up to length-1 bytes. Stop after encountering a newline character (included in the return value), EOF, or having read length -1 bytes (see first). If lengthis not specified, the default is 1 K, or 1024 bytes.
If it fails, it returns false.
<?php$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); Echo fgets ($myfile); fclose ($myfile);? >
FGETC (): reads a single character
Fclose (Resources) Close an open file resource unlink (file) Delete a file
Feof (): Check whether the end has been reached.useful for traversing data of unknown length.
<?php$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); //Output single line until End-of-filewhile (!feof ($myfile)) { echo fgets ($myfile). "<br>";} Fclose ($myfile);? >
FileSize (filename): filename is a file name and isThe string type. The result of this function is cached, and the Clearstatcache () is cleared to clear the cache. FileType ();