Php 打开两个文件夹并显示图像
我遇到了一个问题,就是试图使用php显示数据库中的两个相册照片。目前,以下代码可以工作,但只适用于一张专辑。基本上我也需要它来显示“珠穆朗玛峰第二部分”的照片Php 打开两个文件夹并显示图像,php,Php,我遇到了一个问题,就是试图使用php显示数据库中的两个相册照片。目前,以下代码可以工作,但只适用于一张专辑。基本上我也需要它来显示“珠穆朗玛峰第二部分”的照片 <?php $path = "images/galleries/"; $album = 'mount-everest-part-1'; if ($handle = opendir($path.$album.'/thumbs/')) { while (false !== ($file = readdir($handle))
<?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循环将文件吐出?创建全局数组:
$all = array();
然后,做2个循环并推送全局数组(例如,创建一个读取目录的函数)
最后一步:填充视图
如何使用此代码打开两个文件并使用相同的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>';
}
}
这听起来像是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\u Picture\u File
对象来推送它。查看此内容后,它的作用会更大一些。这个问题很可能涉及面很广。但是,如果您想回答这个问题:文件名旁边需要有文件放置在哪个相册中的信息,以便可以填充视图。我没有时间展示正确的代码,因此删除了我的答案,因为它不完整。在示例中使用$files=[]
可能有点太早了,因为它是5.4版的功能,而且?这非常有效!非常感谢。我的php技能非常基础,所以这是一种我无法理解的方式:(@RobMorris this使用类。类是可以单独添加数据/函数的对象(以及traits/static/等其他东西,但暂时不要担心),它们可以包含自己的私有变量(这就是为什么$this->fileName
每个文件都不一样的原因)。我建议你阅读,因为一旦你了解了它,它会让你大吃一惊。谢谢你的提示。我是一个前端开发人员,所以这对我来说有点吓人!不过我想了解php和php oop。也许我需要一个像你这样的老师;)
$path = "images/galleries/";
$albums = ['mount-everest-part-1', 'mount-everest-part-2'];
foreach ($albums as $album)
{
list_images($path, $album);
}
<?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>';
}
?>