列出目录中的图片(01.png)和说明(01.txt) [英] List images(01.png) and descriptions(01.txt) from directory
问题描述
在存在描述的情况下,如何显示目录中的图像并为每个图像获取相应的描述.
How can I display images from directory and get a corresponding description with each image, give the description exists.
在目录中//
01.png
01.txt
02.png
03.png
03.txt
etc.
显示为//
<img src="01.png"><br>This is the description from the text file named 01.txt
<img src="02.png"><br>
<img src="03.png"><br>This is the description from the text file named 03.txt
我一直在搜索,但是找不到任何东西,因此,如果有人可以将我指向正确的方向,将不胜感激.对于想要创建非常简单的画廊或图像和名称列表的人来说,这也是一件非常有用的事情.
I've been searching and searching, but can't find anything, so if someone could point me in the right direction it would be greatly appreciated. Also this is a very useful thing to be able to do for people wanting to create very simple galleries or lists of images and names.
提前谢谢!
推荐答案
这就是您要查找的内容,因为必须从相应的.txt
文件中动态捕获描述:
This is what you're looking for, as the description must be dynamically captured from a corresponding .txt
file:
$dir = './';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
$filename = pathinfo( $file, PATHINFO_FILENAME) . '.txt';
$description = file_exists( $filename) ? file_get_contents( $filename) : '';
echo '<img src="' . $file . '"><br>' . $description;
}
它的作用是使用 glob()
从给定目录($dir
).然后,对于每个图像,它获取图像的文件名(因此01.png
将为01
),并附加.txt
以获取描述文件的名称.然后,如果描述文件存在,则使用 file_get_contents()
将描述文件加载到$description
变量中.然后输出所需的HTML.
What it does is grabs an array of *.png
files using glob()
from a given directory ($dir
). Then, for each image, it gets the filename of the image (so 01.png
would be 01
), and appends .txt
to get the name of the description file. Then, it loads the description file into the $description
variable using file_get_contents()
if the description file exists. It then outputs the desired HTML.
这篇关于列出目录中的图片(01.png)和说明(01.txt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!