PHP fopen () and file_get_contents () Application and difference introduction _php tips

Source: Internet
Author: User
Tags url example
Copy Code code as follows:

$file =fopen ("11.txt", "R") or exit ("Unable to open file!"); /fopen Open the file, if it does not exist, the display will not open.
$filesize =filesize ("11.txt");//Calculate File size
Echo fread ($file, $filesize);//Read file
Fclose ($file);//Close file

fopen () Opens the file example,

Fclose () is not shown on the page, but if you do not use the fclose (), the file being opened will always occupy resources.
fopen () Open URL Example:
Copy Code code as follows:

$web = "http://www.baidu.com"; http://can't load if you don't add it.
$FP =fopen ($web, ' R ');
if ($fp) {
while (!feof ($fp)) {
Echo fgets ($FP);
}
}

Feof () Check whether the file to the end, to the end to return 1, not to return 0;

Fgets () is read-by-line.

File_get_contents () example;
Copy Code code as follows:

$web = "Http://www.baidu.com"
$fcontent =file_get_contents ($web);
Echo $fcontent;

Obviously file_get_contents () is much simpler.

And in the course of the experiment I found that if the $web = "" is not added to the www. Will jump directly, plus www. will be loaded on this page.

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.