PHP Directory Processing—Browsing Directory and Operating Directory
In this article, we mainly talk about the browsing directory and operating directory of directory processing. Reading and writing operations like file processing, directory processing are similar to file processing, but the operation methods may be slightly different.
The previous article "PHP Directory Processing - Open/Close Directory" introduced the most basic opening and closing of directory processing. Below we will introduce in detail browsing the directory and operating the directory!
—: Browse the directory
To browse files in the directory in PHP, we use the scandir() function. The syntax format of this function is as follows:
array scandir ( string $directory [, int $sorting_order [, resource $context ]] )
This function returns an array containing all files and directories in directory. The parameter sorting_order specifies the sorting order. The default is ascending alphabetical order. If this parameter is added, it will be sorted in descending order.
The following example will view all files in a directory. The specific example code is as follows:
"; //循环输出文件及目录名称 } } else{ echo "路径错误"; } ?>
The running results are as follows:
2: Operating Directory
A directory is a special kind of file, that is to say, most of the operating functions for files (such as renaming) also apply to directories. But there are some special functions that are just for directories. The following list is some commonly used directory operation functions:
Function prototype | Function description | Example |
bool mkdir(string $pathname) | Create a specified directory | mkdir('temp'); |
bool rmdir(string $dirname) | Delete the specified directory, which must be empty | rmdir('tmp') |
string getcwd(void) | Get the current working directory | getcwd() |
bool chdir(string directory) | Change the current directory to directory | echo getcwd()." "; chdir('../');echo getcwd()." " |
float disk_free_space(string directory) | Returns the free space (bytes) of the directory. The files being checked must be accessed through the server's file system | disk_free_space('d:\\appserv'); |
float disk_total_space(string directory) | Return the total space size of the directory (bytes) | disk_total_sapce('d:\\appserv') |
string readdir(resource handle) | Return the file name of the next file in the directory (when using this function, the directory must be opened using the opendir() function). Before PHP5, this function was used to browse directories. | while(false!==($path=readdir($handle))){echo $path;} |
Reassign the specified directory to the beginning of the directory | rewinddir($handle) |
Advanced Applications of PHP File Processing—Remote File Access and Locking Files"!
The above is the detailed content of PHP directory processing—browsing directories and operating directories. For more information, please follow other related articles on the PHP Chinese website!