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

时间:2013-09-25 09:09:35

标签: php

我遇到了尝试使用php从数据库中显示两张照片的问题。目前,以下代码适用于一个专辑。基本上我需要它来显示来自'mount-everest-part-2'以及。

<?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>'; 
}

?>

如何使用此代码打开两个文件并使用相同的foreach循环将文件吐出?

3 个答案:

答案 0 :(得分:1)

这听起来像OOP很适合的那些东西之一。这是一个例子:

<?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>'; 
    }
?>

请注意,我们不是使用字符串推送$files,而是使用Album_Picture_File个对象推送它。

答案 1 :(得分:0)

创建一个全局数组:

$all = array();

然后,制作2个循环并推送全局数组(例如,创建一个读取目录的函数)

$files_dir_one = getFiles("your_dir");
$files_dir_two = getFiles("your_dir2");

$all = array_merge($files_dir_one, $files_dir_two);

function getFiles($directory) {
$files = array();
if ($handle = opendir($directory)) { 
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 

}
//asort($files);
return $files;

}

然后,最后一步:填充视图

答案 2 :(得分:0)

  

如何使用此代码打开两个文件并使用相同的foreach循环将文件吐出?

从字面上看你的问题就像这样。首先使用两个输入变量提取函数:

/**
 * @param string $path
 * @param string $album
 */
function list_images($path, $album) {
    $files = [];

    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>';
    }
}

然后你可以迭代你的两张专辑并输出两个:

$path   = "images/galleries/";
$albums = ['mount-everest-part-1', 'mount-everest-part-2'];

foreach ($albums as $album)
{
    list_images($path, $album);
}