PHP opendir() 仅列出文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6497833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:33:58  来源:igfitidea点击:

PHP opendir() to list folders only

phpopendir

提问by Jeff Thomas

I would like to use opendir() to list only the folders in a particular folder (i.e. /www/site/). I would like to exclude files from the list as well at the '.' and '..' folders that appear on a linux folder listing. How would I go about doing this?

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

回答by StanleyD

foreach(glob('directory/*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace('directory/', '', $dir);
    echo $dir;
}

You can use simply glob with GLOB_ONLYDIR and then filter resulted directories

您可以简单地将 glob 与 GLOB_ONLYDIR 一起使用,然后过滤结果目录

回答by Jason McCreary

Check out the PHP docs for readdir(). It includes an example for exactly this.

查看readdir()PHP 文档。它包括一个例子。

For completeness:

为了完整性:

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

Simply change opendir('.')to your directory, i.e. opendir('/www/sites/'), and update $blacklistto include the names of files or directory you do not wish to output.

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

回答by phihag

function scandir_nofolders($d) {
   return array_filter(scandir($d), function ($f) use($d) {
       return ! is_dir($d . DIRECTORY_SEPARATOR . $f);
   });
}

This function returns an array you can iterate over or store somewhere, which is what 99.37% of all programmers using opendirwant.

这个函数返回一个你可以迭代或存储在某个地方的数组,这是 99.37% 使用的程序员opendir想要的。

回答by T.Todua

List only folders (Directories):

仅列出文件夹(目录):

<?php
$Mydir = ''; ### OR MAKE IT 'yourdirectory/';

foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace($Mydir, '', $dir);
    echo $dir;
}
?>

回答by Chaminda Bandara

Try this with glob('*')function

试试这个glob('*')功能

    <?php
    $dirs = array_filter(glob('*'), 'is_dir');
    $i = 1;
    foreach ($dirs as $value) {
        echo $i . '. &nbsp; <a href = "http://localhost/' . $value . '" target = "_blank">' . $value . '</a><br>';
        $i++;
    }
    ?>

Above code worked for me for list out folders in current directory and I further developed code to open each folder in a new tab in same browser. This is shows only directories.

上面的代码对我有用,用于列出当前目录中的文件夹,我进一步开发了代码以在同一浏览器的新选项卡中打开每个文件夹。这仅显示目录。

回答by Muralikrishnan

Can also be used in forms to create a Dropdown of Folder names (here it is the images folder). Ensures that a user uploading an image pushes it to the correct folder :-)

也可以在表单中使用以创建文件夹名称下拉列表(这里是图像文件夹)。确保上传图像的用户将其推送到正确的文件夹:-)

<select name="imgfolder">
    <option value="genimage">General Image</option>
    <?php
    $Mydir = '../images/'; //  use 'anydirectory_of_your_choice/';

    foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
    $dirname =  basename($dir) ;
    echo '<option value="' . $dirname . '">' . $dirname . '</option>'  ; 
    }
    ?>
    </select>