我如何读取文件夹名称


How can i read folder names?

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

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

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

一定要阅读文档中的陷阱!!

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

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