如何使用php按名称显示图像时显示文件夹中的图像?

时间:2016-03-07 06:44:49

标签: php

我有两个文件夹,1)- 23feb20162)- 23feb2016。 我想从folder1获取一张图片,从folder2获取一张图片,图片名称必须是唯一的,例如userid等。

这是我的HTML代码:

<div class="col-md-12">
    <div class="col-md-6">
        <Image src="../images/registered/20115.jpg" style="width: 100%; height: 100%"></Image> Registered
    </div>
    <div class="col-md-6">
        <Image src="../images/registered/20115.jpg" style="width: 100%; height: 100%"></Image> Attendance
    </div>
 </div>

same image will be display in model and by submiting it will send flag to DB

1 个答案:

答案 0 :(得分:2)

您可以使用以下方法查找唯一文件。

//path to directory to scan
$directory1 = "./folder1/";
$directory2 = "./folder2/";

//get all Image files with a .jpg extension.
$jpgFolder1 = glob($directory1 . "*.jpg");
$jpgFolder2 = glob($directory2 . "*.jpg");

$UniqeImage[];

if(($jpgFolder1 != null) && ($jpgFolder2 != null))
{
    $UniqeImage = array_unique(array_intersect($jpgFolder1, $jpgFolder2));
}

现在,Array Variable $ UniqeImage []包含使用它的唯一文件名,你可以构造HTML

foreach($UniqeImage as $img)
{
    echo("<div class='col-md-12'>");
    echo("    <div class='col-md-6'>");
    echo("        <Image src='../images/Folder1/" . $img . ".jpg' style='width: 100%; height: 100%'></Image> Registered");
    echo("    </div>");
    echo("    <div class='col-md-6'>");
    echo("        <Image src='../images/Folder2/" . $img . ".jpg' style='width: 100%; height: 100%'></Image> Attendance");
    echo("    </div>");
    echo("</div>");
}