Perfidus Posted March 24, 2009 Share Posted March 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/ Share on other sites More sharing options...
wildteen88 Posted March 24, 2009 Share Posted March 24, 2009 So you're uploading images, but when you save the image you remove the file extension! Why? Also by DDBB do you mean Database? Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792535 Share on other sites More sharing options...
Perfidus Posted March 24, 2009 Author Share Posted March 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792543 Share on other sites More sharing options...
Perfidus Posted March 24, 2009 Author Share Posted March 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792548 Share on other sites More sharing options...
Perfidus Posted March 24, 2009 Author Share Posted March 24, 2009 Mybe I can use an array with all allowed extensions and check if file_exists() till it returns true, but the question is: Is it really worth? Isn't better to store the file extension with the name in the in the DDBB? Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792554 Share on other sites More sharing options...
Perfidus Posted March 24, 2009 Author Share Posted March 24, 2009 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."\" />"; } Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792590 Share on other sites More sharing options...
Daniel0 Posted March 24, 2009 Share Posted March 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792595 Share on other sites More sharing options...
Perfidus Posted March 24, 2009 Author Share Posted March 24, 2009 your code lokks clean, but it wont return the extension? Will it? In my code finally I show the image, previously I need to get ext. Anyway, this topic is solved. Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792668 Share on other sites More sharing options...
Daniel0 Posted March 24, 2009 Share Posted March 24, 2009 glob returns the filenames of all files that match, so yes, it should return the extension as well. Quote Link to comment https://forums.phpfreaks.com/topic/150869-solved-get-image-extension-without-knowing-the-extension-thats-it/#findComment-792680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.