Php provides many methods to read files. Next we will introduce some common examples of reading and inputting files. Php provides many methods to read files. Next we will introduce some common examples of reading and inputting files.
Script ec (2); script
1. Read files
First, let's explain what is reading the file itself and what is reading the input content of the file. For example, content in test. php
1. Reading the file itself reads all the content in the file.
2. Read the output content of a file as shown in the file.
Ii. fopen Method
1. Read the file itself
$ Filename = "test. php ";
$ Handle = fopen ($ filename, "r ");
$ Contents = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
Echo strlen ($ contents );
?>
2. Read the output content of the file.
$ Filename = "http: // localhost/test. php ";
$ Handle = fopen ($ filename, "r ");
$ Contents = "";
While (! Feof ($ handle )){
$ Contents. = fread ($ handle, 8192 );
}
Fclose ($ handle );
Echo strlen ($ contents );
?>
The two files read in the preceding figure are the same, but why are they different? http: // localhost/test. php, test. the php file is explained. fopen only obtains the content entered by this script. Please refer to the explanation on the official php website.
Fopen () binds the name resource specified by filename to a stream. If filename is in the format of "scheme: //...", it is treated as a URL. PHP will use the search protocol processor (also known as the Encapsulation Protocol) to process this mode. If the Protocol has not yet registered the encapsulation protocol, PHP will send a message to help check the potential problems in the script and continue executing the filename as a normal file name.
Iii. file Method
1. Read the file itself
$ Filename = "test. php ";
$ Content = file ($ filename); // get the Array
Print_r ($ content );
?>
2. Read the output content of the file.
$ Filename = "http: // localhost/test. php ";
$ Content = file ($ filename );
Print_r ($ content );
?>
Iv. file_get_contents Method
1. Read the file itself
$ Filename = "test. php ";
$ Content = file_get_contents ($ filename); // obtain the string
Echo strlen ($ content );
?>
2. Read the output content of the file.
$ Filename = "http: // localhost/test. php ";
$ Content = file_get_contents ($ filename );
Echo strlen ($ content );
?>
5. readfile Method
1. Read the file itself
$ Filename = "test. php ";
$ Num = readfile ($ filename); // number of returned bytes
Echo $ num;
?>
2. Read the output content of the file.
$ Filename = "http: // localhost/test. php ";
$ Num = readfile ($ filename); // number of returned bytes
Echo $ num;
?>
6. ob_get_contents Method
1. Read the output content of the file.
Ob_start ();
Require_once ('bbb. php ');
$ Content = ob_get_contents ();
Ob_end_clean ();
Echo strlen ($ content );
?>
Summary
Php: There are many methods to read files and there are also many methods to read URLs. I have summarized them. If you have any questions, please correct them. If you have any questions, please add them.