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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)