Home  >  Article  >  Backend Development  >  The difference between php fopen() and file_get_contents() is explained in detail

The difference between php fopen() and file_get_contents() is explained in detail

韦小宝
韦小宝Original
2018-02-27 09:54:193108browse

We often encounter fopen() and file_get_contents() in our PHP development. I believe many students find that these two functions are basically the same, so today we will talk about php fopen What is the difference between () and file_get_contents()? Enough nonsense, let’s take a look!

Reading files in phpYou can use the two functions fopen and file_get_contents. There is no essential difference between the two, but the former php codeis similar to reading files. A little more complicated than the latter. This article explains the implementation code of fopen and file_get_contents to read files through examples.

The code for fopen to read files is as follows:

<?php
$file_name = "1.txt";
echo $file_name . "
";
$fp = fopen($file_name, &#39;r&#39;);
//$buffer=fgets($fp);
while (!feof($fp)) {
$buffer = fgets($fp);
echo $buffer;
}
fclose($fp);
?>

Note that fopen needs to use the fgets and fclose functions to read files.

The code for file_get_contents to read the file is as follows:

<?php
if (file_exists($path)) {
$body = file_get_contents($path);
echo $body; //输入文件内容
} else {
echo "文件不存在 $path";
}
?>

This function reads all the file contents at once and displays them, but if the file is too large, PHP will take up a lot of memory.

Of course, there are also files like file, which generally read files into arrays. At the same time, files can also be read.

Let me introduce fopen() and file_get_contents( ) The difference in usage of opening a URL to obtain web page content

In PHP, if you want to open a web page URL to obtain web page content, the more commonly used functions are fopen() and file_get_contents(). If the requirements are not strict, these two functions can be chosen arbitrarily according to personal preferences in most cases. This article will talk about the differences in the usage of these two functions, as well as the issues that need to be paid attention to when using them.

fopen() opens the URL

The following is an example of using fopen() to open the URL:

<?php
$fh = fopen(&#39;http://www.php.cn/&#39;, &#39;r&#39;);
if($fh){
while(!feof($fh)) {
echo fgets($fh);
}
}
?>

As you can see from this example, after fopen() opens the web page, it returns The $fh 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 line 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 has not been closed by fclose()).

It can be seen that fopen() returns only a resource. If the opening fails, this function returns FALSE.

file_get_contents() opens the URL

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

<?php
$fh= file_get_contents(&#39;http://www.php.cn/&#39;);
echo $fh;
?>

As you can see from this example, after file_get_contents() opens the web page, it returns $fh is a string that can be output directly.

Through the comparison of the above two examples, it can be seen 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, if you are reading a relatively large resource, it is more appropriate to use fopen().

Recommended related articles:

This article mainly introduces a summary of PHP's opening and closing file operation functions. This article explains fopen() and fclose()

Summary of usage of PHP fopen() and fclose() functions

PHP fopen and fwrite functions create html pages. Idea: Use the fopen function and fread function to get the template...

PHP fopen and fwrite functions create html pages

The above is the detailed content of The difference between php fopen() and file_get_contents() is explained in detail. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn