Home > Article > Backend Development > PHP uses fopen and file_get_contents to read file examples, _PHP tutorial
You can use the two functions fopen and file_get_contents to read files in PHP. There is no essential difference between the two. The php code for reading files in the former is a little more complicated than the latter. This article explains the implementation code of fopen and file_get_contents to read files through examples. Coders who need it can refer to it.
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 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 the contents of all files 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
The following will introduce to you the difference in usage between fopen() and file_get_contents() to open the URL and obtain the web content
In PHP, if you want to open a webpage URL and obtain the webpage 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 URL
The following 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 you can see from this example, after fopen() opens a web page, the $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 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 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; ?>
As you can see from this example, after file_get_contents() opens the web page, the $fh returned is a string and 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().