[英]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.jpg
, me@yahoo_1.jpg
, me@yahoo_2.jpg
, you@gmail.jpg
。
My code on showing only one image of user if exist me@yahoo.jpg
and you@gmail.jpg
: 我的代码仅显示用户的一张图片(如果存在
me@yahoo.jpg
和you@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.