This article to the Code farmers to introduce PHP using fopen and file_get_contents read file instance sharing and the difference between these two functions, the need for code farmers can refer to.
PHP read files can use the fopen and file_get_contents these two functions, there is no essential difference between the first, but the former PHP code to read the file is more complicated than the latter. In this article, we explain the implementation code of fopen and file_get_contents reading files through an example. Need to the code farmers can refer to.
Fopen reads the file in the following code:
<?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 fopen read files need to be combined with the fgets and fclose functions.
File_get_contents reads the file in the following code:
<?phpif (File_exists ($path)) {$body = file_get_contents ($path); echo $body;//input file contents} else {echo "file does not exist $path";}? >
This function reads all the contents of the file at once and displays it, but if the file is super-large it causes PHP to occupy a lot of memory.
Of course, like file This is generally the file read the array, but also can be implemented to read the file
Here is a brief introduction to the following fopen () and file_get_contents () open URL to get Web content usage differences
In PHP, to open a Web page URL to get Web content, the more commonly used functions are fopen () and file_get_contents (). If the requirements are not harsh, the two functions in most cases can be selected according to personal preferences, this article discusses the use of the two functions of the difference, and the need to pay attention to the problem.
fopen () Open URL
Here 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 can be seen from this example, fopen () after opening the Web page, the returned $fh is not a string, cannot be output directly, but also need to use the fgets () This function to get the string. The fgets () function reads a row 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 not yet closed by fclose ()).
It is known that fopen () returns only one resource, and if open fails, this function returns FALSE.
file_get_contents () Open URL
Here is an example of using file_get_contents () to open a URL:
<?php$fh= file_get_contents (' http://www.baidu.com/'); Echo $fh;? >
As seen from this example, file_get_contents () after opening the Web page, the returned $FH is a string that can be output directly.
By comparing the above two examples, you can see that using file_get_contents () opens the URL, perhaps more people's choice, because it is simpler and more convenient than fopen ().
However, if you are reading a larger resource, it is more appropriate to use fopen ().
Original address: http://www.manongjc.com/article/618.html
PHP fopen () and file_get_contents () differences introduced