Jump to content

[SOLVED] get image extension without knowing the extension (That's it !)


Recommended Posts

Hi there !

User's of my site can upload pictures to personal folders.

Pictures can be gif, jpg, png, whatever.

The point is: I know the folder and the name of the image but I haven't stored image extension in the DDBB because I thought it was possible to recover it later but now I find myself a little lost, because I was thinking on getimagesize(), but I can't using it without adding the extension...

Is there a way to get the extension of a file by it's name rather than storing the extension in the DDBB. And if it exists, is it better option than storing it it?

No, sorry, I explained myself poorly.

The images on the server folder they have extensions, of course.

But when I upload them I bring then a name based in a numerical reference and that`s what I know, this number, but I'm not storing the extension on the DDB, only this reference.

I thought I was going to be easy to look at the folder, and check for file 7575757 and see what extension does it have, but now I find that I need to know the extension to access the file.

To make it simple, lets say:

1 - I have a folder where only images will exist (no other files)

2 - I know the names, not extensions, of this images and there wont be repeated names, not even with different extensions

3 - I want to know the extension that follows a given name in that folder

So, this is my own solution, if someone can bring some extra light over it, it will be wellcome...

$file = $path.$name.".";
$ext = array("jpg", "jpeg", "JPEG", "gif", "png", "bmp");
for($x = 0; $x < 6; $x++)
{
$image = $file.$ext[$x];
if(file_exists($image))
{
	$pic=$image;
}
}
if(isset($pic))
{
list($width, $height, $type, $attr) = getimagesize($pic);
echo "<img src=\"".$pic."\" width=\"".$width."\" height=\"".$height."\" alt=\"".$name."\" />";
}

if ($results = glob($path . $name . '.*')) {
$filename = $results[0];
}
else {
// doesn't exist...
}

 

This assumes that there will be no duplicates such that e.g. test.jpg and test.png exist though. Your code has the same limitation, however.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.