简体   繁体   English

检查图像文件名是否存在php循环

[英]Check if Image file name exists php loop

I have multiple images on my root folder /images/filename.here . 我的根文件夹/images/filename.here上有多个图像。 Image filenames are like this (email): me@yahoo.jpg , me@yahoo_1.jpg , me@yahoo_2.jpg , you@gmail.jpg . 图片的文件名是这样的(电子邮件): me@yahoo.jpgme@yahoo_1.jpgme@yahoo_2.jpgyou@gmail.jpg

My code on showing only one image of user if exist me@yahoo.jpg and you@gmail.jpg : 我的代码仅显示用户的一张图片(如果存在me@yahoo.jpgyou@gmail.jpg

<? 
$email= $data['payment']['email'];
$receipts = "images/".$email;
     if (file_exists($receipts)) { ?>
                <img src="<?=($receipts)?>" width:"100px" height="50px"/>
                <? } else {
                    echo "no receipt";
    } ?>

this only show the "without _x " filename. 这仅显示“无_x ”文件名。 How can I show other images of a user(eg me@yahoo) using loop _1 , _2 , _3 ... It should display all images that belong to a user. 如何使用循环_1_2_3 ...显示用户的其他图像(例如me @ yahoo),它应显示属于用户的所有图像。

I would create a helper function get_image to return the path of the image by email and index, and use a while loop to build the list of images: 我将创建一个辅助函数get_image以通过电子邮件和索引返回图像的路径,并使用while循环来构建图像列表:

<?php
function get_image($email, $i) {
    $path = "images/receipts/".$email;
    if ($i > 0) $path .= '_' . $i;
    $path .= '.jpg';
    if (file_exists($path)) return $path;
    return null;
}

$i = 0;
while ($path = get_image($email, $i)) {
    echo "<img src=\"$path\" width:\"100px\" height=\"50px\"/>";
    $i++;
} 

if ($i == 0) {
    echo "no receipt";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM