php read file

In the previous section, we talked about file operations. Reading files is one of the most commonly used functions.

readfile reads a file

So how to read a file? Let's learn a function first.

int readfile (string $filename)

Function: Pass in a file path and output a file.

In the following code, the file is read as long as the file name or the specified file path is passed in.

Note: The windows slash in the above code is \slash, which may escape some characters. Therefore, when we write, we write two slashes.

file_get_contentsopen file

The above is a direct output after simply opening the file. Is there any operation method that can be assigned to a variable after opening the file?

PHP will certainly provide this method. This method is one of the ways PHP opens a file and returns the content. Let’s take a look at the function:

string file_get_contents (string filename)

Function: Pass in a file or file path, opening this file returns the contents of the file. The content of the file is a string.

The above code opens a file and outputs the contents of the file.

Let’s expand the code based on the previous knowledge. Use your previous knowledge.

'; } ?>

Above, we have combined the knowledge we have learned before.

fopen, fread, and fclose operations read files

The above file_get_contents method of opening a file is simple and crude. The following

resource fopen (string $file name, string mode)

string fread (resource $operation resource, int read length)

bool fclose (resource $operation resource)

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

1 .Open the resource

2.Use related functions to operate

3.Close the resource

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

1. The path to open the file

2. Open the file The pattern

The return type is a resource type. For the first time, we encountered the resource type mentioned in the previous basic type.
The resource type requires other functions to operate this resource. All resources must be closed when they are opened.

fread functionThe function of the function is to read the open file resource. Read the file resource of the specified length, read part of it and move part backward. to the end of the file.

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

After understanding the functions, the last two functions are relatively simple. What are the modes of the fopen function? The modes of fopen are as follows. Let’s talk about the modes of fopen:

Mode Explanation
r Open in read-only mode and point the file pointer to the file header.
#r+ Open in read-write mode and point the file pointer to the file header.
w Open writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it
w+ Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it
a Open it in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it
a+ Open it in 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 Create it and open it for writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create
x+

to create it and open it in read-write mode, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it

#Next, we will only learn r mode, and we will write Let’s talk about other modes later.

We must first know how to read files before we can master writing files well.

1.Open the file

2.Read the file

3.Close the file

Other notes:

Mode Instructions t Convert \n to \ under windows r\n ##b
Binary open mode
Description:



The experiment cannot allow the naked eye to see the experimental effect. Just remember this feature.

Windows provides a text conversion tag ('t') that can transparently convert \n to \r\n.

Corresponding to this, you can also use 'b' to force binary mode so that the data will not be converted. To use these flags, use either 'b' or 't' as the last character of the mode argument.


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!