Home>Article>Backend Development> Summary of knowledge on closing and opening directories in PHP file processing

Summary of knowledge on closing and opening directories in PHP file processing

WBOY
WBOY Original
2021-11-02 10:21:29 3222browse

In the previous article "How to read files in PHP", I gave you a detailed introduction to the relevant knowledge about reading files in PHP file processing. In this article, we will also learn about PHP. Knowledge of file handling, but instead of handling files, it handles directories. Let's take a look at directory processing in PHP. I hope everyone has to help!

Summary of knowledge on closing and opening directories in PHP file processing

#Before we introduced some knowledge about file processing in PHP, let’s take a look at the related knowledge about directory processing. To be precise, directory processing is also a part of file processing. The directory can be regarded as a special file. We can only view the files in this directory by opening the directory first. When our file processing is completed, the indispensable step is to close the directory.

The most important thing is to open and close the directory. Let's take a look at how to open and close the directory in PHP.

opendir() Function, open the directory

Think To open a directory, you need to use theopendir()function.

When it comes to opening a directory, you will think of the operation of opening a file we talked about before. To open a file, you need to pass thefopen()function. At that time, we mentioned that when usingfopen( )When the function opens a file, if the target file does not exist, or if there is no target file on the current page, this function will create a file.

When we use theopendir()function to open the target directory, if the target directory does not exist, or the current page cannot find the target directory, it will not be created. directory, this function will generate an error.

After understanding the difference between theopendir()function to open a directory and the fopen() function to open a file, let’s take a look at the basic syntax format of the opendir() function:

opendir(string $path[, resource $context])

What we need to pay attention to is that the parameter $path represents the directory path that needs to be opened. If this path is correct, the program will return a pointer to the directory after running; if the directory path is not correct, or the path is correct but This directory cannot be opened because of an error in the system permission file system. At this time, the function will return an error message or false.

If you want to block the output of this error message, you can add "@" in front of the opendir() function.

Next, let’s take a look at the application of the opendir() function through an example. The example is as follows:

In the above example, the path you want to open through the opendir() function isD:\phpstudy_pro\WWWdirectory.

It should be noted that is_dir() in the above example is used to determine whether the current path is a directory. The output result of the above example is as follows:

Summary of knowledge on closing and opening directories in PHP file processing

Let’s introduce another way to express the path. The example is as follows:

The directory is in the same directory as the current file

Summary of knowledge on closing and opening directories in PHP file processing

Output result:

Summary of knowledge on closing and opening directories in PHP file processing

#Through the above example, we have completed the operation of opening the directory, and opened different files through the opendir() function. Directory under path expression. Let me introduce to you how to close the directory.

closedir()function, close the directory

Think To close a directory, you need to use theclosedir()function.

In the above example, we opened the directory. After we complete the corresponding operation, we want to release the resources used in the operating directory. At this time, closing the directory is essential. Let's take a look at the basic syntax format of the closedir() function:

closedir([resource $dir_handle])

What we need to pay attention to is: the parameter handle is a directory pointer opened using the opendir() function.

Next, let’s take a look at the usage of theclosedir()function through an example. The example is as follows:

Output result:

Summary of knowledge on closing and opening directories in PHP file processing

There is no change in the output result, but there is an additional operation of closedir() to close the directory.

If you are interested, you can click on "PHP Video Tutorial" and "How to upload files in PHP? You’ll understand after reading it!》Learn more about PHP knowledge.

The above is the detailed content of Summary of knowledge on closing and opening directories in PHP file processing. 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