2017-06-12 13 views

答えて

1

あなたは基本的に解決策を書いています。 globは実際に一致するファイル名を返します。

$result = glob('uploads/logo_'.$row['id'].'.*'); 

// glob returns an empty array if no matching files are found, 
// so we need to check if the result is empty or not. 
if (is_array($result) && count($result) > 0) { 

    // We got at least one match, let's use the first one 
    echo '<p><img src="'. $result[0] .'" height="75"></p>'; 
} 
1

グロブを使用すると、ファイル拡張子を含むファイルの完全なパスが既に存在します。最初に表示するには

-

$result = glob('uploads/logo_'.$row['id'].'.*'); 
if(!empty($result)) 
{ 
    foreach ($result as $file) { 
     if (@is_array(getimagesize($file))) { 
      $relativePath = end(explode('/', $file)); 
      echo '<p><img src="uploads/' . $relativePath . '" height="75"></p>'; 
     } 
    } 
} 

:すべての一致した画像を表示するには

:私はその場で、ここでいくつかのコードを(私はまだそれをテストしていない)書かれている

一致するイメージ:

$result = glob('uploads/logo_'.$row['id'].'.*'); 
if(!empty($result)) 
{ 
    $file = current($result); 
    if (@is_array(getimagesize($file))) { 
     $relativePath = end(explode('/', $file)); 
     echo '<p><img src="uploads/' . $relativePath . '" height="75"></p>'; 
    } 
} 

私は画像ファイルを除外しています。画像ファイル以外のファイルがないと確信できる場合は、@is_array(getimagesize($file))の条件をスキップしてください。

関連する問題