PHP opendir() 仅列出文件夹

发布于 2024-11-17 04:43:25 字数 104 浏览 7 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离不开的别离 2024-11-24 04:43:25

查看 readdir() 的 PHP 文档。它包含一个具体的示例。

为了完整起见:

<?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 包含您不希望输出的文件或目录的名称。

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

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 $blacklist to include the names of files or directory you do not wish to output.

只等公子 2024-11-24 04:43:25
foreach(glob('directory/*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace('directory/', '', $dir);
    echo $dir;
}

您可以简单地使用 glob 和 GLOB_ONLYDIR ,然后过滤结果目录

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

花开柳相依 2024-11-24 04:43:25
function scandir_nofolders($d) {
   return array_filter(scandir($d), function ($f) use($d) {
       return ! is_dir($d . DIRECTORY_SEPARATOR . $f);
   });
}

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

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 opendir want.

我是男神闪亮亮 2024-11-24 04:43:25

仅列出文件夹(目录):

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

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

List only folders (Directories):

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

foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace($Mydir, '', $dir);
    echo $dir;
}
?>
初心 2024-11-24 04:43:25

使用 glob('*') 函数尝试

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

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

Try this with glob('*') function

    <?php
    $dirs = array_filter(glob('*'), 'is_dir');
    $i = 1;
    foreach ($dirs as $value) {
        echo $i . '.   <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.

征棹 2024-11-24 04:43:25

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

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文