请参阅我尝试过的以下代码。如何打印目录和子目录的列表以及子目录中的文件
echo scanDirectoryImages("Images");
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg',
'gif', 'png'))
{
$html = '';
if (
is_readable($directory)
&& (file_exists($directory) || is_dir($directory))
) {
$directoryList = opendir($directory);
while($file = readdir($directoryList)) {
if ($file != '.' && $file != '..') {
$path = $directory . '/' . $file;
if (is_readable($path)) {
if (is_dir($path)) {
return scanDirectoryImages($path, $exts);
}
if (
is_file($path)
&& in_array(end(explode('.', end(explode('/', $path)))),
$exts)
) {
$html .= '<img src="' . $path
. '" style="max-height:200px;max-width:200px"
value="<?php echo basename($path)"/> ';
}
}
}
}
closedir($directoryList);
}
return $html;
}
它只返回从我的子目录中的图像,而不是我真正想要的子目录的路径。 此外,我在一个服务器上工作,而不是本地主机,这是创建一个问题,以设置$路径。 请帮忙!!!
这是一个递归函数。如果你使用参数('jpeg','jpg','png','gif')调用函数,那么它只会列出图像。 您需要做的不仅仅是复制粘贴代码,而是理解它并对其进行修改,使其符合您的需求。 ($ path)){ return scanDirectoryImages($ path,$ exts);你对此感兴趣。只是为了向您指出正确的方向 – Nico