0

I'm trying to echo an image from a folder called avatar_50x50, with the name of the file being the username. Although the extension will always be an image, how will I determine what file type it is?

<img src="../images/users/avatar_50x50/<?php echo $sel_user['username']; ?>"
Justin
  • 87
  • 1
  • 4
  • 8
  • store the name of the image in the db, it's better than searching the image – Bgi Sep 13 '12 at 15:54
  • possible duplicate of [how to check a file path is an image or not in php?](http://stackoverflow.com/questions/2117396/how-to-check-a-file-path-is-an-image-or-not-in-php) – Ignacio Vazquez-Abrams Sep 13 '12 at 15:55
  • I have no table in my database for files. What's the alternative? – Justin Sep 13 '12 at 15:56
  • Duplicates: [1](http://stackoverflow.com/questions/10130804/checking-image-file-type) and [2](http://stackoverflow.com/questions/9547113/check-image-type-if-renamed-from-png-to-jpg) and there are certainly more... – Jocelyn Sep 13 '12 at 16:06

2 Answers2

1

you can use glob() and take first element from array if you are sure there is only one image with name as username:

<?php
    $file = glob('../images/users/avatar_50x50/'.$sel_user['username'].'.*');
    echo '<img src="../images/users/avatar_50x50/'.$file[0].'" alt="" />';
?>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

Use http://php.net/exif_imagetype

And than use "switch" operator to find right image ext.

$type = exif_imagetype($image);
switch($type) {
  case IMAGETYPE_GIF:
    $ext = '.gif';
  // etc...
}
MrSil
  • 608
  • 6
  • 12