List images(01.png) and descriptions(01.txt) from

2019-07-19 13:39发布

How can I display images from directory and get a corresponding description with each image, give the description exists.

in Directory //

01.png
01.txt
02.png 
03.png 
03.txt 
etc.

to display as //

<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.

Thanks in advance!

5条回答
该账号已被封号
2楼-- · 2019-07-19 13:54

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

查看更多
干净又极端
3楼-- · 2019-07-19 13:58

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.

查看更多
时光不老,我们不散
4楼-- · 2019-07-19 14:04

I assume you have the .php file in the same directory as the pictures and text files are.

You could use function glob() to read in all image-files as array from the directory, cut off the file extension (so '01.png' becomes '01') and append the file extension with string concatentation.

A working code example may look like this:

<?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");
        }
    }

?>

So definitely have a look on these functions:

Happy coding.

查看更多
放我归山
5楼-- · 2019-07-19 14:05

Updated version of ZnArKs code, as he missed that you wanted the content of the files

//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/>";
    }
}
查看更多
手持菜刀,她持情操
6楼-- · 2019-07-19 14:08

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

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.

查看更多
登录 后发表回答