Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read Files in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to read files in PHP". In daily operation, I believe many people have doubts about how to read files in PHP. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to read files in PHP". Next, please follow the editor to study!

When we use a computer, we all use the computer to open and close a file, create a file, or upload a file. So can these operations be done in the code? If it can be done, what should be done? In fact, it is very simple, learning file processing is actually a function of learning file processing, combined with what you have learned before. So let's take a look at it next.

Reading files is one of the most common operations in our daily use, so how to implement it in the code.

Here's how to use functions in PHP to read the contents of a file.

Fgetc (): reads a character from a file

Before using these functions to read the file, we need to create a text file under the server root directory (here we take text.txt as an example), the text content is as follows:

Next, let's take a look at how fgetc () should be used in use, and what the output is:

Output result:

Fgetc can only read one character, and we can combine it with the loop mentioned earlier to get the following example:

Output result:

Fgets () and fgetss (): read files line by line

Output result:

The fgetss () function is a variant of fgets (), which is also used to read a row of data. While reading, it also filters out the PHP and HTML tags in the read content.

As you can see, the file can be read line by line with fgets, and as above, we can use a loop to read all the files.

Examples are as follows:

Output result:

Fread (): read the file (any length)

Output result:

Readfile (): read all files

Output result:

File (): reads the entire file into an array

Output result:

File_get_contents (): reads the file into a string

First, let's create a test.txt file

Then enter:

By combining with what we have learned before, we output the results:

This is the integration of what we have learned, first by opening the file through file_get_contents, and then through the list each statement and while loop to get the output.

Extended knowledge: fopen, fread, fclose operation read

The way the above file_get_contents opens the file is simple and rough, so the next few

Resource fopen (string $filename, string mode) string fread (resource $operation resource, int read length) bool fclose (resource $operation resource)

Through the above function, we will explain the common operation of resource types:

Open a resource

Use related functions to operate

Close the resource

The function of the fopen function fopen function is to open a file. There are two main parameters:

The path to which the file is opened

The mode of opening the file

The return type is a resource type, and the resource type requires other functions to manipulate the resource. All resources are opened and closed.

Fread function: the function of the function is to read open file resources. Read a file resource of a specified length, read a part and move a part backward. To the end of the file.

Fclose function: the function of the fclose function is to close resources. Resources are opened and closed.

Next, let's introduce the mode of the fopen function:

R Open read-only and point the file pointer to the file header.

Open r + read-write mode and point the file pointer to the file header.

W open in write mode, point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create

W + open read-write mode, point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create

An open in write mode and point the file pointer to the end of the file. If the file does not exist, try to create

Open a + read-write mode and point the file pointer to the end of the file. If the file does not exist, try to create it

X creates and opens it as a write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns FALSE with an error message at the E_WARNING level. If the file does not exist, try to create

X + creates and opens it as read-write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns FALSE with an error message at the E_WARNING level. If the file does not exist, try to create

At this point, the study of "how to read files in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report