Scorptique Posted November 21, 2008 Share Posted November 21, 2008 Hey all, I need some help ^^;; I want to store my images in the database then display it in a gallery. The image schema is: Image(id, name) The way I thought of is to connect to database and extract the image name, (eg. rose). Then I will echo it out echo "<img src=images/$name.jpg></img><br>"; But what happens if my images have different extensions? like .gif, .png, .bmp. How am I suppose to display my images? How do I extract the extension? I can't manually store the extension names because it's not me who is uploading the pictures. I am doing a web application which allows users to upload their images and I dont expect them to type in the image extension in a different field everytime they want to upload sth. Help guys, thanks! ^^;; Link to comment https://forums.phpfreaks.com/topic/133617-displaying-images-with-different-extensions/ Share on other sites More sharing options...
ratcateme Posted November 21, 2008 Share Posted November 21, 2008 you can store the extension when a user uploads a file in php in the $_FILES array there is a field called name. if you go $ext = substr($_FILES['uploadedfile']['name'] $ext = substr($_FILES['uploadedfile']['name'],strrpos($_FILES['uploadedfile']['name'],".")+1); it will output the extension. you can save that in the database and pull it out when displaying the image. Scott. Link to comment https://forums.phpfreaks.com/topic/133617-displaying-images-with-different-extensions/#findComment-695169 Share on other sites More sharing options...
Scorptique Posted November 21, 2008 Author Share Posted November 21, 2008 Thanks scott, I sort of understand what you're trying to say. But does this only work when the user uploads a file? What about my other images I added into the folder initially to get my website going? Do I have to manually key in the extensions? Thanks. Link to comment https://forums.phpfreaks.com/topic/133617-displaying-images-with-different-extensions/#findComment-695511 Share on other sites More sharing options...
ratcateme Posted November 21, 2008 Share Posted November 21, 2008 are all these images in the same folder? you could do something like this $files = glob("*.*"); $valid_exts = array("jpg","jpeg","gif","png","tiff"); foreach($files as $file){ $ext = substr($file,strrpos($file,".")+1); if(in_array($ext,$valid_exts)){ echo "$file is a image<br>"; $name = substr($file,0,strrpos($file,".")); echo "File is called {$name}<br>"; $query = "UPDATE `images` SET `ext` = '{$ext}' WHERE `name` = '{$name}'"; mysql_query($query); } } it should update all your existing images. Scott. Link to comment https://forums.phpfreaks.com/topic/133617-displaying-images-with-different-extensions/#findComment-695692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.