Use fopen in PHP to share with file_get_contents to read files.

Source: Internet
Author: User
This article describes how to use fopen and file_get_contents in PHP to read information shared by file instances. For more information, see fopen and file_get_contents functions in php, there is no essential difference between the two, but the php code for reading files in the former is a little more complicated than that in the latter. 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.

The following describes how to use fopen () and file_get_contents () to open a URL to obtain webpage content.

In php, to open a webpage URL to obtain webpage content, the common functions are fopen () and file_get_contents (). If the requirements are not strict, these two functions can be selected based on your hobbies in most cases. This article describes the differences between the usage of these two functions and the issues that need to be paid attention to during use.

Fopen () open URL

The following is an example of opening a URL using fopen:

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

From this example, we can see that after fopen () opens the webpage, $ fh returned is not a string and cannot be output directly. you also need to use the fgets () function to obtain the string. The fgets () function reads a row from the object pointer. The file pointer must be valid and must be directed to a file successfully opened by fopen () or fsockopen () (not closed by fclose ).

Fopen () returns only one resource. if it fails to be opened, this function returns FALSE.

File_get_contents () open URL

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

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

In this example, after file_get_contents () opens the webpage, $ fh is returned and can be output directly.

Through the comparison of the above two examples, we can see that using file_get_contents () to open the URL may be the choice of more people, because it is simpler and more convenient than fopen.

However, it is appropriate to use fopen () to read large resources.

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.