2016-06-16 12 views
0

私のルートフォルダに複数の画像があります/images/filename.here。画像ファイル名は次のとおりです(電子メール):[email protected][email protected]_1.jpg[email protected]_2.jpg[email protected]画像ファイル名が存在するかどうかを確認する

存在する場合、ユーザーの1つの画像のみを示す上の私のコード[email protected][email protected]

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

これだけ「なし_x」ファイル名を表示します。ループ_1_2_3 ...を使用して、ユーザーの他の画像(me @ yahooなど)を表示するにはどうすればいいですか?ユーザーに属するすべての画像が表示されるはずです。

+1

場合、カウンターでwhileループを作成します。カウンタが0の場合、$ filename = $ email(0以上の場合)、$ filename = $ email。 "_"。$ counter。ファイル名が存在しない場合は、whileを終了します。 – theatlasroom

答えて

1

私は、電子メールやインデックスで画像のパスを返し、イメージのリストを構築するために、whileループを使用するヘルパー関数get_imageを作成します。

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

ここにいくつかの変更が必要です。とにかくありがとう。 –

関連する問題