


How to traverse all files in a specified directory? (php file system operation)
May 20, 2017 pm 05:01 PMHow to traverse all files in a specified directory? (php file system operation)
In the acquired management system of the website, it is often necessary to manage and maintain the files in the website server. Sometimes it is necessary to add a folder and delete a certain file. folders or files. In order to better view these files or folders, it is necessary to traverse the files in the specified directory of the server. So how to implement the traversal function of files in the specified directory?
The key to traversing all files in a specified directory is the application of directory and file processing functions, including file_exists(), is_dir(), opendir() and closedir() functions.
1. Application function analysis
(1) file_exists(): Check whether the file or directory exists. The syntax is as follows:
1 |
|
If the file or directory specified by filename exists, return true, otherwise return false.
(2) is_dir(): Determine whether the specified file name is a directory. The syntax format is as follows:
1 |
|
If the filename exists and is a directory, true is returned. If filename is a relative path, its relative path is checked according to the current working directory.
Description:
The result of this function will be cached, and it cannot be applied to remote files and is checked The files must be accessed through the server's file system.
(3) opendir(): Open the directory handle, the specific syntax format is as follows:
1 |
|
This function returns a directory handle, which can be closedir(), readdir( ) is called before. If path is not a legal directory or the directory cannot be opened due to permission restrictions or file system errors, opendir() returns false and generates an error message.
(4) readdir(): Read entries from the directory handle. The specific syntax format is as follows:
1 |
|
This function returns the file name of the next file in the directory. The file name starts with Sorting returns in the file system.
(5) closedir(): Close the directory handle. The specific format is as follows:
1 |
|
This function closes the directory stream specified by dir_handle. The directory stream must have been opened by opendir().
Using the above functions comprehensively, you can realize the file traversal operation in the specified directory.
2. Application Example
In this example, apply the function introduced above to realize the traversal operation of files in the specified directory. The specific The steps are as follows:
(1) Create a php file, add a form, define the form elements, and submit the path to the directory through the post method.
(2) In this php file, determine whether the submitted directory path is empty, then open the directory, read the files in the directory, and output the read files, close the directory, otherwise the output directory does not exist , the specific code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Run this example, enter a specified folder in the text box, click the "Submit" button, if the folder exists, you can display the files included in the folder All files, the running results are as follows:
The above is the detailed content of How to traverse all files in a specified directory? (php file system operation). For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
