Php 简明教程
PHP – Listing Files
Windows 命令 DIR 和 Linux 命令 ls 都显示当前目录中的文件列表。可以使用不同的交换机操作这些命令,以便对显示的文件列表应用条件。PHP 提供了一些用于以编程方式列出给定目录中文件的方法。
Windows command DIR and Linux command ls both display the list of files in the current directory. These commands can be operated with different switches to apply conditions on the list of files displayed. PHP provides a couple of options for programmatically listing files in a given directory.
The readdir() Function
PHP 中的 opendir() 函数类似于 fopen() 函数。它返回目录句柄,以便可以按序列化方式读取目录的内容。
The opendir() function in PHP is similar to fopen() function. It returns handles to the directory so that the contents of the directory can be read from in a serialized manner.
opendir(string $directory, ?resource $context = null): resource|false
此函数打开一个目录句柄,用于后续的 closedir()、readdir() 和 rewinddir() 调用。
This function opens up a directory handle to be used in the subsequent closedir(), readdir(), and rewinddir() calls.
readdir() 函数读取 opendir() 函数返回的流句柄中的下一个可用条目。
The readdir() function reads the next available entry from the stream handle returned by opendir() function.
readdir(?resource $dir_handle = null): string|false
此处, dir_handle 是先前使用 opendir() 打开的目录句柄,如果未指定,则假定为由 opendir() 打开的最后一个链接。
Here, dir_handle is the directory handle previously opened with opendir().not specified, the last link opened by opendir() is assumed.
closedir() 函数类似于 fclose() 函数。它关闭目录句柄。
The closedir() function is similar to fclose() function. It closes the directory handle.
closedir(?resource $dir_handle = null): void
该函数关闭 dir_handle 指示的目录流。该流必须先前由 opendir() 打开。
The function closes the directory stream indicated by dir_handle. The stream must have previously been opened by opendir().
Example
以下 PHP 代码从当前登录目录中一次读取一个文件。
The following PHP code reads one file at a time from the currently logged directory.
<?php
$dir = getcwd();
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename:" . $file . "\n";
}
closedir($dh);
}
}
?>
The scandir() Function
scandir() 函数检索给定目录中的文件和子目录。
The scandir() function retrieves the files ans subdirectories inside a given directory.
scandir(string $directory,
int $sorting_order = SCANDIR_SORT_ASCENDING,
?resource $context = null): array|false
“sorting_order” 默认按升序排列。如果将此可选参数设置为 SCANDIR_SORT_DESCENDING,则排序顺序变为按降序排列。如果它设置为 SCANDIR_SORT_NONE,则结果将变为未排序。
The "sorting_order" by default is alphabetical in ascending order. If this optional parameter is set to SCANDIR_SORT_DESCENDING, then the sort order becomes alphabetical in descending order. If it is set to SCANDIR_SORT_NONE, then the result becomes unsorted.
Example
使用以下 PHP 代码,scandir() 函数返回给定目录中的文件数组。
With the following PHP code, the scandir() function returns an array of files in the given directory.
<?php
$dir = "c:/xampp/php/mydir/";
$files = scandir($dir);
var_dump($files);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(5) "a.txt"
[3]=>
string(5) "b.txt"
}
你可以使用 foreach 循环遍历 scandir() 函数返回的数组。
You can use a foreach loop to traverse the array returned by the scandir() function.
<?php
$dir = "c:/xampp/php/mydir/";
$files = scandir($dir);
foreach ($files as $file)
echo $file . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
.
..
a.txt
b.txt