0

I have the path to an image file /files/uploads/1 but as you can see, I don't have an extension so I can't display the image. How can I get the extension of the file so I can display it? Thanks!

EDIT: Here is come code I have tried. BMP, PNG, JPG, JPEG and GIF are the only possible extensions, but $path ends up never getting assigned a value.

$exts = array('bmp','png','jpg','jpeg','gif');
foreach ($exts as $ext) {
    if (file_exists("/files/uploads/" . $id . "." . $ext)) {
        $path = "/files/uploads/" . $id . "." . $ext;
    }
}
Josh
  • 86
  • 1
  • 2
  • 14
  • Your code will only work if the files have the extensions which is what you're trying to find out in the first place. Your best bet in this case is to try to find the MIME type by reading the file header or something. – Maximus2012 Aug 25 '15 at 21:07
  • See if this helps: http://php.net/manual/en/function.image-type-to-mime-type.php – Maximus2012 Aug 25 '15 at 21:07
  • Possible duplicate: http://stackoverflow.com/questions/7925962/how-to-get-the-image-type-in-php – Maximus2012 Aug 25 '15 at 21:08
  • possible duplicate of [How to extract a file extension in PHP?](http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php) – l'L'l Aug 25 '15 at 21:09
  • @Maximus2012 I didn't use the exact method you suggested, but thanks for a list of more image file types. I only had a few so I expanded it to include those too. – Josh Aug 25 '15 at 21:26

3 Answers3

0

Personally, I had a problem where I had an unnecessary slash (/) before my path in the file_exists check, but the solution to the question is still the same.

$exts = array('bmp','png','jpg','jpeg','gif','swf','psd','tiff','jpc','jp2','jpx','jb2','swc','iff','wbmp','xbm','ico');
foreach ($exts as $ext) {
    if (file_exists("files/uploads/" . $id . "." . $ext)) {
        $path = "/files/uploads/" . $id . "." . $ext;
    }
}
Josh
  • 86
  • 1
  • 2
  • 14
0

You don't need a file extension to show an image.

<img src="/files/uploads/1">

Will show the image. It's the same way the placehold.it images work

<img src="http://placehold.it/350x150">
Arielle Lewis
  • 540
  • 4
  • 14
  • This is what my program was originally doing, but it didn't work. That's why I was looking for a way of getting the extension in the first place. – Josh Aug 25 '15 at 21:47
0

First of all , I think when you get the file name for the image that means no need to add extension , so you have the file name and image folder

it should simply look like this :

$fullpath = ('uploadword/'.$filename);
// then call it 
<img  src='".$fullpath."'> 

The normal scenario to upload all the images in specific folder then get the filename and save it in data base

move_uploaded_file($file_tmp,"upload/".$filename);

Some professional make it more sophisticated to make filename unique ,because they expected some duplicated values and many reasons , here is one simple technique :

$anynameorfunction = "";// random number etc...
$newname = $anynameorfunction.$filename;
move_uploaded_file($file_tmp,"upload/".$newname);

So, when they call it again it should be simple like this

  $fullpath = ('uploadword/'.$newname);
 // then call it 
  <img  src='".$newname."'> 

This is in very simple way and i wish you get what i mean

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18