PHP uses fopen to read file instances with file_get_contents to share _php instances

Source: Internet
Author: User
Tags php code

Reading files in PHP can use the fopen and file_get_contents functions, there is no essential difference between the two, but the former read the file PHP code is more complicated than the latter. This article through an example to explain fopen and file_get_contents read the implementation code of the file. Need the Code farmers can refer to.

fopen read the file code 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 fopen read files need to be used in conjunction with the fgets and fclose functions.

File_get_contents read the file code as follows:

<?php
if (file_exists ($path)) {
$body = file_get_contents ($path);
Echo $body; Input file Contents
} else {
echo ' file does not exist $path ';
}

This function is to read all the file contents at once and display it, but if the file super assembly causes PHP to account for a large amount of memory.

And, of course, like file. This is generally read the file as a group, but also can be implemented to read the file

Below to introduce the next fopen () and file_get_contents () open the URL to get the content of the Web page usage difference

In PHP, to open a Web page URL to get the content of the page, the more commonly used functions are fopen () and file_get_contents (). If the requirements are not harsh, the two functions in most cases can be based on the choice of individual preferences, this article discusses the use of the two functions of what the difference, as well as the need to pay attention to the problem.

fopen () Open URL

Here is an example of using fopen () to open a URL:

<?php
$fh = fopen (' http://www.baidu.com/', ' R ');
if ($FH) {while
(!feof ($fh)) {
echo fgets ($FH);
}
? >

As you can see from this example, when fopen () opens a Web page, the returned $fh is not a string, cannot be exported directly, and the fgets () function is needed to get the string. The fgets () function reads a row from the file pointer. The file pointer must be valid and must point to a file that was successfully opened by fopen () or fsockopen () and not yet closed by fclose ().

As you know, fopen () returns just a resource, and if it fails, this function returns FALSE.

file_get_contents () Open URL

Here is an example of using file_get_contents () to open a URL:

<?php
$fh = file_get_contents (' http://www.baidu.com/');
echo $fh;
? >

From this example, file_get_contents () opens the page, and the returned $FH is a string that can be exported directly.

By comparing the above two examples, you can see that using file_get_contents () to open the URL may be more people's choice because it is simpler and more convenient than the fopen ().

However, if you are reading larger resources, it is more appropriate to use fopen ().

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.