dongliao3450 2013-09-25 09:09
浏览 16
已采纳

打开两个文件夹并显示图像php

I've got the problem of trying to display two albums of photos from a db using php. Currently the following code works but for just one album. Basically I need it to display photos from 'mount-everest-part-2' aswell.

<?php

$path = "images/galleries/";
$album = 'mount-everest-part-1';

if ($handle = opendir($path.$album.'/thumbs/')) { 
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
}
asort($files);

foreach($files as $file) { 
        echo '<li><a href="../' . $path . $album . '/images/' . $file . '" rel="shadowbox['.$album.']"><img src="../' . $path . $album . '/thumbs/' . $file . '" /></a></li>'; 
}

?>

How can I use this code to open two files and spit the files out using the same foreach loop?

  • 写回答

3条回答 默认 最新

  • dongzha5934 2013-09-25 09:38
    关注

    This sounds like one of those things that OOP would suit nicely. Here's an example:

    <?php
        class Album_Picture_File {
            private $fileName;
            private $path;
            private $album;
    
            public function __construct($fileName, $path, $album) {
                $this->fileName = $fileName;
                $this->path = $path;
                $this->album = $album;
            }
    
            private function getAlbumPath() {
                return '../' . $this->path . $this->album;
            }
    
            public function getPicturePath() {
                return $this->getAlbumPath() . '/images/' . $this->fileName;
            }
    
            public function getThumbnailPath() {
                return $this->getAlbumPath() . '/thumbs/' . $this->fileName;
            }
        }
    
        function fetchFiles($path, $album) {
            $files = array();
            if ($handle = opendir($path.$album.'/thumbs/')) { 
                while (false !== ($file = readdir($handle))) {
                    if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') { 
                        $fullPath = $path . $album . '/thumbs/' . $file;
                        $files[$fullPath] = new Album_Picture_File($file, $path, $album); 
                    } 
                } 
                closedir($handle); 
            }
            ksort($files); //sort after key (out file path)
            return $files;
        }
    
        $files = array_merge(
            fetchFiles('images/galleries/', 'mount-everest-part-1'),
            fetchFiles('images/galleries/', 'mount-everest-part-2')
        );
    
        foreach($files as $file) { 
            echo '<li><a href="' . $file->getPicturePath() . '" rel="shadowbox['.$album.']"><img src="' . $file->getThumbnailPath() . '" /></a></li>'; 
        }
    ?>
    

    Note that instead of pushing $files with strings, we push it with Album_Picture_File objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'