PHP uses fopen and file_get_contents to read file instance sharing, _php tutorial

Source: Internet
Author: User

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


PHP read files can use the fopen and file_get_contents these two functions, there is no essential difference between the first, but the former PHP code to read the file is more complicated than the latter. In this article, we explain the implementation code of fopen and file_get_contents reading files through an example. Need to the code farmers can refer to.

Fopen reads the file in the following code:

Note fopen read files need to be combined with the fgets and fclose functions.

File_get_contents reads the file in the following code:

This function reads all the contents of the file at once and displays it, but if the file is super-large it causes PHP to occupy a lot of memory.

Of course, like file This is generally the file read the array, but also can be implemented to read the file

Here is a brief introduction to the following fopen () and file_get_contents () open URL to get Web content usage differences

In PHP, to open a Web page URL to get Web content, 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 selected according to personal preferences, this article discusses the use of the two functions of the difference, and 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 can be seen from this example, fopen () after opening the Web page, the returned $fh is not a string, cannot be output directly, but also need to use the fgets () This function 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 ()).

It is known that fopen () returns only one resource, and if open 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;? >

As seen from this example, file_get_contents () after opening the Web page, the returned $FH is a string that can be output directly.

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

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

Articles you may be interested in:

    • Analysis of the relationship between CPU 100% and file_get_contents function of php-cgi process
    • In-depth PHP function file_get_contents time-out processing method detailed
    • Parsing file_get_contents in PHP gets the problem of remote page garbled
    • File_get_contents ("Php://input", "R") instance introduction
    • PHP read local file common functions (fopen and file_get_contents)
    • PHP file_get_contents Setting Timeout processing method
    • How PHP uses file_get_contents to read large files

http://www.bkjia.com/PHPjc/1106118.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106118.html techarticle PHP uses fopen and file_get_contents to read the file instance sharing, PHP read the file can use the fopen and file_get_contents these two functions, there is no essential difference between the former, just read ...

  • 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.