laitimes

PHP 目录和遍历目录函数

Table of contents and traversal directory functions

The following functions are all functions based on directory (folder) operations

mkdir() to create a new directory

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )           

Permission mode: effective under Linux, directory permissions. This parameter is ignored for win.

If the parent directory of the created directory does not exist, whether to create recursively, use the third parameter to create recursively (true);

Under the Linux system, mkdir recursively creates directories, and the compatibility is not good;

Write a custom function: Create a directory recursively on Linux

function Directory( $dir ){
    return is_dir ( $dir ) or Directory(dirname( $dir )) and mkdir ( $dir , 0777);
}           

rmdir() to delete the directory

bool rmdir ( string $dirname [, resource $context ] )           

An attempt was made to delete the directory specified by dirname. The directory must be empty and have the appropriate permissions. Failure results in a E_WARNING-level error.

Traversal directory function

opendir() — 打开目录句柄

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

Opens a directory handle that can be used in subsequent closedir(), readdir(), and rewinddir() calls.

readdir() — Read an entry from a directory handle

string readdir ( resource $dir_handle )           

Returns the file name of the next file in the directory. The file name is returned in order in the file system.

closedir() — 关闭目录句柄

void closedir ( resource $dir_handle )           

Close the directory stream specified by dir_handle. The stream must have been opened by opendir() before.

rewinddir() 倒回目录句柄

void rewinddir ( resource $dir_handle )           

Reset the directory stream specified by dir_handle to the beginning of the directory.

<?php
$dir = "/etc/php5/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>           

A simple way to read the directory structure

scandir - Lists files and directories in the specified path

array scandir ( string $directory [, int $sorting_order [, resource $context ]] )           

Returns an array containing files and directories in the directory.

directory:要被浏览的目录

sorting_order: The default sort order is alphabetical ascending (default is 0, ascending). If you use the optional parameter sorting_order (0 is set to 1), the sort order is in descending alphabetical order.

context:context参数的说明见手册中的 Streams API 一章。

Return value: Returns an array containing the file name if successful, or FALSE if it fails. If directory is not a directory, it returns a Boolean value of FALSE and generates a E_WARNING-level error.

<?php
$dir = '/tmp' ;
$files1 = scandir ( $dir );
$files2 = scandir ( $dir , 1 );

print_r ( $files1 );
print_r ( $files2 );
?>           

The output of the above routine looks like this:

Array
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)
Array
(
[0] => somedir
[1] => foo.txt
[2] => bar.php
[3] => ..
[4] => .
)           

Delete directory rmdir()

bool rmdir ( string $dirname [, resource $context ] )           

An attempt was made to delete the directory specified by dirname. The directory must be empty and have the appropriate permissions. Failure results in a E_WARNING-level error.

dirname:目录的路径。

context:Note: Added support for context in PHP 5.0.0. For a description of Context, see Streams.

<?php
if (! is_dir ( 'examples' )) {
mkdir ( 'examples' );
}

rmdir ( 'examples' );
?>