- $ Path1 = "F:/www/jbxue.txt ";
- If (! File_exists ($ path1 )){
- Echo "the file does not exist! ";
- } Else {
- $ Handle1 = fopen ($ path1, 'R + ') or exit ("Unable to open file ");
- // While (! Feof ($ handle1 )){
- // Echo fgets ($ handle1 )."
";
- //}
- While (! Feof ($ handle1 )){
- Echo fgetc ($ handle1 );
- }
- }
- ?>
The above provides a simple file read operation.
Note: fopen is used to open file resources. Usage: $ file = fopen ("welcome.txt", "r"); specifically, the first parameter is the file path. The following parameters are the methods required to open a file: 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 clear the file content. if the file does not exist, create a new file. W + read/write. Open and clear the file content. if the file does not exist, create a new file. A append. Open and write to the end of the file. if the file does not exist, create a new file. A + read/append. Write content to the end of the file to keep the file content. X write only. Create a new file. If the file already exists, FALSE is returned. X + read/write. Create a new file. If the file already exists, FALSE and an error are returned.
Note: If fopen () cannot open the specified file, 0 (false) is returned ). The first four are commonly used. Fgetc: string fgetc (resource $ handle) returns a string containing a character, which is obtained from the file pointed to by handle. If EOF is encountered, FALSE is returned. Fgets: string fgets (int $ handle [, int $ length]) reads a row from the file pointed to by handle and returns a string of a maximum length of 1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes. If an error occurs, FALSE is returned. Fgetss: string fgetss (resource $ handle [, int $ length [, string $ allowable_tags]) and fgets () are the same, only fgetss tries to remove any HTML and PHP markup from the read text. (Same as fgets (), it only filters html and php tags .) You can use the optional third parameter to specify which tags will not be removed. The feof () function checks whether it has reached the end of the file (eof ).
// Determine whether a file or directory contains bool file_exists (string filename) and whether a file or directory exists. If yes, the system returns TRUE. Otherwise, the system returns the false format: if(file_exists(%hello.txt ")) {Echo "file exists";} // open file format: fopen (filename, mode) description: Open the specified file in the specified format filename: file name to be opened mode: open the fopen(‑hello.txt‑form, and then use the "write" method to open the hello.txt file // write file format: fwrite (resource, string); note: add the specified content resource to the Open File: Open file string: content to be written example: $ handle = fopen(“hello.txt "," w ") // if a, you can append data to fwrite ($ handle," 1 \ r \ n ") // close the file format: fclose ($ handle) Example: $ handle = fopen(“hello.txt "," w "); fclose ($ handle); // read a data row in the format of fgets (int handle [, int length]) description: read length-1 characters. If length is not specified, the default byte is 1 kb. if a newline, EOF, or character is read, the program terminates. If an error occurs, false is returned;12. Next page |