PHP uses fopen and file_get_contents to read file examples, _PHP tutorial

WBOY
Release: 2016-07-12 08:57:59
Original
755 people have browsed it

PHP uses fopen and file_get_contents to read file examples.

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:

<&#63;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);
&#63;> 
Copy after login

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:

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

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:

<&#63;php
$fh = fopen('http://www.baidu.com/', 'r');
if($fh){
while(!feof($fh)) {
echo fgets($fh);
}
}
&#63;>
Copy after login

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:

<&#63;php
$fh= file_get_contents('http://www.baidu.com/');
echo $fh;
&#63;>
Copy after login

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().

Articles you may be interested in:

  • Analysis of the relationship between PHP-CGI process CPU 100% and file_get_contents function
  • In-depth explanation of the method of timeout processing of PHP function file_get_contents
  • The problem of parsing file_get_contents in PHP to obtain garbled remote pages
  • file_get_contents("php://input", "r") example introduction
  • php common functions for reading local files ( fopen and file_get_contents)
  • PHP file_get_contents setting timeout processing method
  • php method of using file_get_contents to read large files

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1106118.htmlTechArticlePHP uses fopen and file_get_contents to read file examples. To read files in php, you can use fopen and file_get_contents. Function, there is no essential difference between the two, except that the former reads...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!