PHP Directory processing-Browse directories and manipulate directories
In this article, we mainly talk about directory processing browse directory and operation directory, like file processing read, write operations, directory processing is similar to file processing, but the operation method may be a little different.
In the previous article, "PHP directory processing-open/Close directory", the most basic open and close directories for directory processing are described. Below we will detail the browse directory, Operation directory!
-: Browse Directory
In PHP Browse directory of the file, we use the Scandir () function, the function syntax format is as follows:
Array Scandir (string $directory [, int $sorting _order [, Resource $context]])
The function returns an array that contains all the files and directories in the directory. The Sorting_order parameter specifies the sort order, which is sorted alphabetically by default and, if added, becomes a descending sort.
The following instance will look at all the files in a directory, with the following example code:
<?phpheader ("content-type:text/html; Charset=utf-8 "), $path =" D:\phpStudy\WWW\php ", if (Is_dir ($path)) { //detection is a directory $dir = Scandir ($path); Use the Scandir () function to get all files and directories foreach ($dir as $value) { //using the Foreach Loop echo $value. " <br> "; Loop output file and directory name }} else{ echo "Path error";}? >
The results of the operation are as follows:
II: Operation Directory
The directory is a special kind of file, that is, the operation of the file function (such as: rename) Most of the same applies to the directory, but there are some special functions are only specific to the directory, the following list is some common directory operation functions:
Function prototypes |
Function description |
Example |
BOOL MkDir (String $pathname) |
Create a new directory of the specified |
mkdir (' temp '); |
BOOL RmDir (String $dirname) |
Deletes the specified directory, which must be empty |
RmDir (' tmp ') |
String getcwd (void) |
Get the current work directory |
GETCWD () |
BOOL ChDir (String directory) |
Change the current directory to directory |
Echo getcwd (). " <br> "; ChDir ('.. /'); echo getcwd (). " <br> " |
float disk_free_space (String directory) |
Returns the free space for the directory (bytes). The files being inspected must be accessed through the server's file system |
Disk_free_space (' D:\\appserv '); |
float disk_total_space (String directory) |
Returns the total space size of the directory (bytes) |
Disk_total_sapce (' D:\\appserv ') |
String Readdir (Resource handle) |
Returns 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 the directory. |
while (false!== ($path =readdir ($handle))) {echo $path;} |
void Rewinddir (Resource handle) |
Reassign the specified directory to the beginning of the directory |
Rewinddir ($handle) |
PHP directory processing to the end of the first, we continue to introduce PHP file processing, please read the "PHP File processing advanced Applications-remote file access and lock files"!