我怎样才能读取文件夹名称?

时间:2016-08-08 10:30:33

标签: php directory

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

是否有任何函数或方法用PHP获取文件夹名称?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

只需一个简单的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);
}
?>

请务必阅读陷阱的文档!!!