PHP opendir() 仅列出文件夹

标签 php opendir

我想使用 opendir() 仅列出特定文件夹(即/www/site/)中的文件夹。我想从列表中排除文件以及“。”和出现在 linux 文件夹列表中的“..”文件夹。我该怎么做呢?

最佳答案

查看 PHP docs for readdir() .它包括一个例子。

为了完整性:

<?php
if ($handle = opendir('.')) {
    $blacklist = array('.', '..', 'somedir', 'somefile.php');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?>

只需将 opendir('.') 更改为您的目录,即 opendir('/www/sites/'),并更新 $blacklist 来包含您不想输出的文件或目录的名称。

关于PHP opendir() 仅列出文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6497833/

相关文章:

c - C中的目录问题

php - 文件名、目录名或卷标语法不正确。 (代码 : 123)

php - laravel 护照撤销和修剪事件监听器没有执行任何操作

php - 如何根据从同一查询的内连接收到的值左连接表

PHP:删除目录的安全方法?

c - 传递打开的目录作为参数

linux - perl 中的 "Copy failed: File too large"错误

php - 使用 Ajax、MySQL 和 PHP 实现跟随系统

php - 获取数组中 20% 元素的方法 - PHP

php - 什么是蛞蝓?