Php uses fopen and file_get_contents To Read File instances,
The fopen and file_get_contents functions can be used to read files in php. There is no essential difference between the two functions, but the php code used to read files in php is a little more complicated than that used in php. This article provides examples to illustrate how fopen and file_get_contents can read files. For more information about the coders, see.
The code for fopen to read files is as follows:
<? Php $ file_name = "1.txt"; echo $ file_name. ""; $ fp = fopen ($ file_name, 'R'); // $ buffer = fgets ($ fp); while (! Feof ($ fp) {$ buffer = fgets ($ fp); echo $ buffer;} fclose ($ fp);?>
Note that fopen must be used together with fgets and fclose functions to read files.
The code for file_get_contents to read files is as follows:
<? Phpif (file_exists ($ path) {$ body = file_get_contents ($ path); echo $ body; // input file content} else {echo "the file does not exist $ path" ;}?>
This function reads and displays all the file content at a time, but if the file is too large, php occupies a large amount of memory.
Of course, files like files are generally read as arrays, and files can also be read.