打开两个文件夹并显示图像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?

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.

Create a global array :

$all = array();

Then, make 2 loops and push the global array (create a function that reads the directory for example)

$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;

}

And then, the last step : populate the view

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

Taking your question literally it works like this. First of all extract a function with the two input variables:

/**
 * @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>';
    }
}

Then you can just iterate over your two albums and output both:

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

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