2

如何显示目录中的图像并获得每个图像的相应描述,给出描述存在。

在目录中 //

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

我一直在搜索和搜索,但找不到任何东西,所以如果有人能指出我正确的方向,将不胜感激。对于想要创建非常简单的画廊或图像和名称列表的人来说,这也是一件非常有用的事情。

提前致谢!

4

5 回答 5

3

这就是您要查找的内容,因为必须从相应的.txt文件中动态捕获描述:

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

它的作用是从给定目录 ( )中获取*.png文件数组。然后,对于每个图像,它会获取图像的文件名(因此会是),并附加以获取描述文件的名称。然后,如果描述文件存在,它将描述文件加载到变量中。然后它输出所需的 HTML。glob()$dir01.png01.txt$descriptionfile_get_contents()

于 2012-06-27T17:23:54.373 回答
2

我假设您的 .php 文件与图片和文本文件位于同一目录中。

您可以使用函数glob()从目录中读取所有图像文件作为数组,切断文件扩展名(因此“01.png”变为“01”)并使用字符串连接附加文件扩展名。

一个工作代码示例可能如下所示:

<?php
    $path_to_directory = './';
    $pics = glob($path_to_directory . '*.png');
    foreach($pics as $pic)
    {
        $pic = basename($pic, '.png'); // remove file extension
        echo '<img src=\"{$pic}.png\"><br>'; 
        if(file_exists($pic . '.txt'))
        {
            echo file_get_contents("{$pic}.txt");
        }
    }

?>

所以一定要看看这些功能:

快乐编码。

于 2012-06-27T17:25:35.247 回答
1

Your question is a bit confusing.

make an array with all information.

$pics = array('img' => '01.png', 'text' => 'This is the description');

foreach($pics as $pic) {
    echo '<img src="'.$pic['name'].'" alt="">' . $pic['text'];
}

So you have to put your information in an array or a database otherwise you cannot map the desciption to your image.

When you want to read dynamicly the folder its a bit difficult.

You can look at readdir or glob then you can read all images get the name and load the textfile with file_get_contents but i think its not a really performant way.

于 2012-06-27T17:18:24.143 回答
0

更新版本的 ZnArKs 代码,因为他错过了您想要文件的内容

//path to directory to scan
$directory = "../images/team/harry/";

//get all image files with a .jpg extension.
$images = glob($directory . "*.png");

//print each file name
foreach($images as $image)
{
    $textfile = substr($image, 0, -3) . "txt";

    echo "<img src='{$image}'><br/>";

    if(file_exists($textfile))
    {
       echo file_get_contents($textfile) . "<br/>";
    }
}
于 2012-06-27T17:24:41.013 回答
0

Code Modified from here : http://php.net/manual/en/function.readdir.php

//path to directory to scan
$directory = "../images/team/harry/";

//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");

//print each file name
foreach($images as $image)
{
    print "<img src=\"$image\"><br>This is the description from the text file named $image";
}

ok, so this won't print the contents of the text file, but I'm sure you can further modify the code above to figure that out

YEP

于 2012-06-27T17:19:23.737 回答