【问题标题】:How can i read folder names?如何读取文件夹名称?
【发布时间】:2016-08-08 10:30:32
【问题描述】:

我有一个名为 categories 的文件夹,里面有很多文件夹。我需要把这些名字插入到数据库中。

是否有任何函数或方法可以使用 php 获取文件夹名称?

【问题讨论】:

标签: php directory


【解决方案1】:

只需一个简单的 RTM 即可:readdir()

<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($entry = readdir($handle)) {
        echo "$entry\n";
    }

    closedir($handle);
}
?>

请务必阅读有关问题的文档!!!

【讨论】:

    【解决方案2】:

    您可以使用此代码获取所有文件夹名称

    <?php
        $dirs = array_filter(glob('*'), 'is_dir');
        print_r( $dirs);
    ?>
    

    【讨论】:

    • 试试这个代码,你会得到数组中的所有目录名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    相关资源
    最近更新 更多