【发布时间】:2016-08-08 10:30:32
【问题描述】:
我有一个名为 categories 的文件夹,里面有很多文件夹。我需要把这些名字插入到数据库中。
是否有任何函数或方法可以使用 php 获取文件夹名称?
【问题讨论】:
-
readdir 是你的朋友
我有一个名为 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);
?>
【讨论】: