2017-06-12 104 views
0

任何擴展我正在尋找一種方式,能夠顯示一個圖像文件,不知道分機。 我找到了解決辦法找到一個文件,但不知道它; S延伸,現在我需要的擴展。使用用於顯示圖像文件

$result = glob('uploads/logo_'.$row['id'].'.*'); 
if(is_array($result)) 
{ 
    echo '<p><img src="uploads/logo_'.$row['id'].'.???" height="75"></p>'; 
} 

回答

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

那麼,當你使用glob時,你已經有文件的完整路徑,包括文件擴展名。

我已經寫在這裏的一些代碼上飛(我還沒有測試它尚未):

要顯示所有匹配的圖像:

$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))條件