从多个目录拉出所有图像,并使用PHP显示它们 [英] Pull all images from multiple directories and display them with PHP

查看:124
本文介绍了从多个目录拉出所有图像,并使用PHP显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的服务器上有一个名为'images'的文件夹,在该文件夹中,我可以有一个文件夹,多达10个包含图像的文件夹。



而不是为每个图片编写标签

 < img SRC = 图像/人/ 001.JPG > 
< img src =images / landscape / 001.jpg>

等等



我可以使用PHP以获取主目录图像中所有文件夹中的所有图像?



我对PHP很少有经验,所以这是我正在努力的事情。



我需要php返回一个数组'< div class =box>< img src =images / FOLDER / IMAGENAME.jpg>< / div> '



也许有人可以帮忙。

解决方案

  function ListFiles($ dir){
if($ dh = opendir($ dir)){
$ files = Array();
$ inner_files = Array();
while($ file = readdir($ dh)){
if($ file!=。&&file $ file!=..&& $ file [0] !='。'){
if(is_dir($ dir。/。$ file)){
$ inner_files = ListFiles($ dir。/。$ file);
if(is_array($ inner_files))$ files = array_merge($ files,$ inner_files);
} else {
array_push($ files,$ dir。/。$ file);
}
}
}
closedir($ dh);
返回$文件;
}
}
foreach(ListFiles('/ path / to / images')as $ key => $ file){
echo< div class = \ box\>< img src = \$ file\/>< / div>;
}

这样的事情


I have a folder on my server called 'images', and within that folder I could have a single folder to as many as 10 folders that contain images.

Instead of writing a tag for each image

<img src="images/people/001.jpg">
<img src="images/landscape/001.jpg">

etc etc

Can I use PHP to get all the images in all the folders in the main directory 'images'?

I have VERY little experience with PHP, so this is something I am struggling with.

I need php to return an array of '<div class="box"><img src="images/FOLDER/IMAGENAME.jpg"></div>'

Maybe someone can help.

解决方案

function ListFiles($dir) {
    if($dh = opendir($dir)) {
        $files = Array();
        $inner_files = Array();
        while($file = readdir($dh)) {
            if($file != "." && $file != ".." && $file[0] != '.') {
                if(is_dir($dir . "/" . $file)) {
                    $inner_files = ListFiles($dir . "/" . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else {
                    array_push($files, $dir . "/" . $file);
                }
            }
        }
        closedir($dh);
        return $files;
    }
}
foreach (ListFiles('/path/to/images') as $key=>$file){
    echo "<div class=\"box\"><img src=\"$file\"/></div>";
}

Something like this?

这篇关于从多个目录拉出所有图像,并使用PHP显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆