perky416 Posted November 12, 2011 Share Posted November 12, 2011 Hi everyone, Sorry not sure if this is a php or html problem. Im using php and a html form to upload images to my site, i have made it so that jpg, jpeg, gif and png images can be uploaded. The problem im having is displaying the image. <img src="images/<?php echo $name ?>.jpg" /> That works fine if a jpg was uploaded, but what if a png or a gif was uploaded? The $name is going to be unique, there will not be more than one image with the same name, so what do i have to do to display the image regardless of what the extension is? Thanks Quote Link to comment Share on other sites More sharing options...
silkfire Posted November 12, 2011 Share Posted November 12, 2011 When a user uploads a file you get the full name right, before you change it to something random? To extract extension use the pathinfo() function: $extension = pathinfo('testfile.jpg', PATHINFO_EXTENSION); Quote Link to comment Share on other sites More sharing options...
sunfighter Posted November 12, 2011 Share Posted November 12, 2011 Not the complete code but this will get your extension and as you can see limit the uploads to just those extensions. This is php: if (!empty($_FILES["image"]["name"])) { $image=$_FILES['image']['name']; $filename = stripslashes($_FILES["image"]["name"]); $extension = getExtension($filename); $extension = strtolower($extension); if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $hidden_text = ' Not a valid image format, only use .png, .gif, or .jpg!'; //extension is okay }else Quote Link to comment Share on other sites More sharing options...
perky416 Posted November 12, 2011 Author Share Posted November 12, 2011 Hi guys, Thanks but i managed to solve it shortly after posting. Like a muppet i forgot i was actually saving the URL of the image in the database, i simply recalled the url as the image SRC. Thanks. Quote Link to comment 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.