rondog Posted April 30, 2008 Share Posted April 30, 2008 I know how to upload images, but I am trying to check for the file type extension, and no matter what I do my function is returning false even if its jpg, jpeg, gif or png. Can you see anything I am doing wrong? <?php $uploaded_image = $_FILES['imgfield']['name']; $name = $_POST['namefield']; if(!$name || !$uploaded_image) { $error = "<span class=\"error\">Please fill in all fields.</a>"; } else { function open_image ($file) { // Get extension $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': $im = @imagecreatefromjpeg($file); $image_type = ".jpg"; break; case '.jpeg': $im = @imagecreatefromjpeg($file); $image_type = ".jpeg"; break; case '.gif': $im = @imagecreatefromgif($file); $image_type = ".gif"; break; case '.png': $im = @imagecreatefromgif($file); $image_type = ".png"; break; default: $im = false; break; } return $im; } $image = open_image($uploaded_image); if ($image === false) { $error = "<span class=\"error\">error.$extension</a>"; } else { $width = imagesx($image); $height = imagesy($image); $success = "<span class=\"success\">Successfully added.$width + $height</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/ Share on other sites More sharing options...
mrdamien Posted April 30, 2008 Share Posted April 30, 2008 $_FILES['imgfield']['name'] is not a file that exists. It is just the name of the file that you want to upload. So you cant create the image from a file that's not there :-/. First you need to move_uploaded_file the file onto your server, then you can open it with your function. case '.png': $im = @imagecreatefromgif($file); $image_type = ".png"; break; should be case '.png': $im = @imagecreatefrompng($file); $image_type = ".png"; break; And you can shorten it to: function open_image ($file) { // Get extension $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': $im = imagecreatefromjpeg($file); $image_type = ".jpg"; break; case '.gif': $im = imagecreatefromgif($file); $image_type = ".gif"; break; case '.png': $im = imagecreatefrompng($file); $image_type = ".png"; break; default: $im = false; break; } return $im; } Quote Link to comment https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/#findComment-530060 Share on other sites More sharing options...
rondog Posted April 30, 2008 Author Share Posted April 30, 2008 but if I do move_uploaded_file, isnt that uploading it? I dont want it to be uploaded unless its a specific size. That essentially what im trying to do. $_FILES['imgfield']['name'] is not a file that exists. It is just the name of the file that you want to upload. So you cant create the image from a file that's not there :-/. First you need to move_uploaded_file the file onto your server, then you can open it with your function. case '.png': $im = @imagecreatefromgif($file); $image_type = ".png"; break; should be case '.png': $im = @imagecreatefrompng($file); $image_type = ".png"; break; And you can shorten it to: function open_image ($file) { // Get extension $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': $im = imagecreatefromjpeg($file); $image_type = ".jpg"; break; case '.gif': $im = imagecreatefromgif($file); $image_type = ".gif"; break; case '.png': $im = imagecreatefrompng($file); $image_type = ".png"; break; default: $im = false; break; } return $im; } Quote Link to comment https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/#findComment-530062 Share on other sites More sharing options...
mrdamien Posted April 30, 2008 Share Posted April 30, 2008 Oh sorry, I've lost my head. You don't need to use move_uploaded_file. $_FILES['imgfield']['name'] is the name of the file, but $_FILES['imgfield']['tmp_name'] is the actual file that does exist. And no, move_uploaded_file doesn't upload the file. The file is already uploaded to the temporary directory, move_uploaded_file just moves and renames the temporary file so that it won't get deleted at the end of the script. You could add a paramater to your function for the tmp_name. <?php function open_image ($file, $tmp) { // Get extension $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': $im = imagecreatefromjpeg($tmp); $image_type = ".jpg"; break; case '.gif': $im = imagecreatefromgif($tmp); $image_type = ".gif"; break; case '.png': $im = imagecreatefrompng($tmp); $image_type = ".png"; break; default: $im = false; break; } return $im; } $tempname = $_FILES['imgfield']['tmp_name'] ; $image = open_image($uploaded_image, $tempname); ?> (not tested). Quote Link to comment https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/#findComment-530067 Share on other sites More sharing options...
rondog Posted April 30, 2008 Author Share Posted April 30, 2008 ohh ok I think I was just figuring the 'name' / 'tmp_name' out when you posted this. The server I am working on isnt letting me change the folder permissions to 777 through filezilla. It keeps reverting back to 666, so I cant even test this now..great thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/#findComment-530068 Share on other sites More sharing options...
RichardRotterdam Posted April 30, 2008 Share Posted April 30, 2008 just to add a little something. you dont need the file extension you can get the file type instead //if this is an image it will have the value "image/gif" "image/jpeg" or "image/png" $_FILES['imgfield']['type']; Quote Link to comment https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/#findComment-530206 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.