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


opening two folders and displaying images php

我遇到了尝试使用 php 显示数据库中的两个照片相册的问题。目前,以下代码有效,但仅适用于一个专辑。基本上,我也需要它来显示"珠穆朗玛峰第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 循环将文件吐出?

听起来像是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对象推送它。

创建一个全局数组:

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

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

如何使用此代码打开两个文件并使用相同的 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);
}