Home>Article>Backend Development> How to get all the data of the file at one time in php file operation

How to get all the data of the file at one time in php file operation

青灯夜游
青灯夜游 Original
2021-08-05 15:41:50 5044browse

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:

How to get all the data of the file at one time in php file operation

First let's understandfread() Function, take a look at the following example:

Output result:

How to get all the data of the file at one time in php file operation

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$lengthcharacters in the open file$handlelength 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:

How to get all the data of the file at one time in php file operation

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 asechoto 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_pathand$context.

  • $include_pathParameter: 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'.

  • $contextParameter: 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!

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