Example of PHP File Reading and reading file output content-PHP source code

Source: Internet
Author: User
Tags php website
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.