PHP如何获取文件夹下所以子文件夹的名称及子夹所以图片

PHP如何获取文件夹下所以子文件夹的名称并显示点击其中名称打开名称文件夹下的所以图片并显示
2026年09月25日 09:43
有2个网友回答
网友(1):

先写个函数来遍历文件夹下的所有子文件:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false ) {
if ( $file != ".." && $file != "." ) {
if ( is_dir($dir . "/" . $file) ) {
$files[$file] = scandir($dir . "/" . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}

若你想看图片什么的,可以给已遍历出来的文件名加个链接,链接地址要正确哦!这样当你点击后就可以显示了,具体你可以自己实现一下

网友(2):

$dir = '你的图片根目录';
function listImages($dir)
{
$images = array();
$entries = glob($dir . '/*');

if(!empty($entries)) foreach($entries as $ent) {
if($ent == '.' || $ent == '..') continue;
if(is_dir($ent)) {
$images = array_merge($images, listImages($ent));
} else if( substr(mime_content_type($ent), 0, 5) == 'image' ) {
$images[] = $ent;
} else {
continue;
}
}

return $images;
}
var_dump(listImages($dir));