Home>Article>Backend Development> How to get all the data of the file at one time in php file operation
In the previous article "php file operation - reading files line by line", we introduced the method of reading files line by line, and then using loops we can obtain all the data of the file. So if you don't use a loop, how to get all the data of the file? This article will introduce to you how to obtain all the data of a file at once without using loops.
There are many ways to read all the data of a file at once in PHP. Today we will mainly learn about the two functions fread() and readfile().
We have a text file named "test.txt", the content of which is:
First let's understandfread() Function, take a look at the following example:
Output result:
It can be seen that all the contents of the "test.txt" file are output .
In fact, thefread($handle,$length)
function is to read the specified$length
characters in the open file$handle
length of characters. But when the value of the $length parameter is specified as "filesize($file)
", then all data of the entire file can be read. The role of the filesize() function is to obtain the file size, and the role of the fread() function is to read the entire file.
Using fread() to read the entire file requires opening the file first, calculating the size of the entire file, and then closing the file after reading. It is complicated. Is there any simpler method? The answer is yes, you can use thereadfile() function.
Let’s take a look at the following example:
Output result:
Isn’t it very simple, just use one line of code "readfile($file);
" can obtain all the data of the file at one time; there is no need to open or close the file, and there is no need to use output statements such asecho
to output the file content.
Let’s take a look at thereadfile() function.
readfile($filename,$include_path,$context)
The function reads a file and writes it to the output buffer, and returns the number of bytes read from the file.
This function accepts 1 required parameter$filename
(specify the file name or file path to be read), 2 omitted parameters$include_path
and$context
.
$include_path
Parameter: Set whether you want to search for the file in include_path (in php.ini), the default is FALSE; if you want, set The parameter value is '1'.
$context
Parameter: Set the environment of the file handle
If the reading fails, the readfile() function will return FALSE with an error message; we can hide the error output by adding a '@
' in front of the function name.
@
is an error control operator. When placed before a PHP expression, any error information that may be generated by the expression will be ignored.
Okay, that’s all. If you want to know anything else, you can click this. → →Basic operation of PHP files
## Recommended:《PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of How to get all the data of the file at one time in php file operation. For more information, please follow other related articles on the PHP Chinese website!