Get the App
SLTechnology News&Howtos  ›  Development  › 

Fopen() and file_get_contents() in PHP are applications and differences.

Shulou Source: shulou.com Published: 2022-06-03 15:45:08 11月14日 Update

This article mainly explains that "fopen () and file_get_contents () in PHP are applications and differences". The explanation in this article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "fopen () and file_get_contents () in PHP are applications and differences."

The copy code is as follows:

$file=fopen ("11.txt", "r") or exit ("Unable to open file!"); / / fopen opens the file and shows that it cannot be opened if it does not exist.

$filesize = filesize ("11.txt"); / / calculate the file size

Echo fread ($file,$filesize); / / read the file

Fclose ($file); / / close the file

Example of fopen () opening a file

Whether fclose () is used or not is not reflected on the page, but if you don't use fclose (), the opened file will always take up resources.

Example of fopen () opening a URL:

The copy code is as follows:

$web= "http://www.baidu.com"; / / http:// cannot be loaded without adding it.

$fp=fopen ($web,'r')

If ($fp) {

While (! feof ($fp)) {

Echo fgets ($fp)

}

}

Feof () checks whether the file reaches the end, returns 1 to the end, and does not return 0

Fgets () is read line by line.

File_get_contents () example

The copy code is as follows:

$web = "http://www.baidu.com"

$fcontent=file_get_contents ($web)

Echo $fcontent

Obviously file_get_contents () is simpler.

And in the course of the experiment, I found that if you don't add www in $web = "". Will jump directly, add www. Will be loaded on this page.

Thank you for your reading, the above is the content of "fopen () and file_get_contents () in PHP is the introduction of applications and differences". After the study of this article, I believe you have a deeper understanding of the introduction of applications and differences between fopen () and file_get_contents () in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Files codes differences applications examples learning no content end size that is ideas situations articles more knowledge knowledge points articles web sites resources Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Shulou Information Redmi macOS