merylvingien Posted December 29, 2010 Share Posted December 29, 2010 Hi folks, just upgrading one of my sites with a new script which i thought i had working, well it did at least on my local server. Its an image upload script. It fails at the get image extension part, which when echoed out is blank. Which is why its failing. Can anyone see what i have missed? if($_POST){ $image =$_FILES["image"]["name"]; $img = $_FILES['image']['tmp_name']; $size = filesize($_FILES["image"]["tmp_name"]); $maxsize= 400; if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>409600){ echo 'Upload FAILED, file is too large !'; exit(); } if ($size > $maxsize*1024){ echo "File too big"; exit(); } else { if(file_exists($existingimage)){ unlink($existingimage); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $extension = getExtension($image); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo "The extension is:" . $extension ." "; echo'Unknown Image extension'; exit(); } Ive ended the script here at the part where it fails to reduce the amount of uneeded code to sift through. It fails no matter what type of image is used to upload. Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/ Share on other sites More sharing options...
the182guy Posted December 29, 2010 Share Posted December 29, 2010 I'd do the extension part like this: <?php function getExtension($filename) { $out = false; $pos = strrpos($filename, '.'); if($pos !== false && $pos > 0) { // filename has a . and the . is not the first char $out = substr($filename, $pos + 1); } return $out; } $file = 'test.jpg'; $allowed = array( 'jpg', 'png', 'gif', 'bmp' ); $ext = getExtension($file); if($ext !== false) { // it's a valid extension, does it match a known one? if(in_array(strtolower($ext), $allowed)) { // $ext is one of the allowed extensions echo 'allowed'; } else { // $ext is valid but is not in the allowed list echo 'valid but no allowed'; } } else { // extension is not valid echo 'not valid'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152761 Share on other sites More sharing options...
requinix Posted December 29, 2010 Share Posted December 29, 2010 What's the value of $_FILES["image"]["error"] Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152763 Share on other sites More sharing options...
merylvingien Posted December 29, 2010 Author Share Posted December 29, 2010 Thanks for the replies, but still no joy, no matter what i do, i cannot get the ext to echo out. Its like any image i try has no extension to it. Obviously missing something drastic here. Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152788 Share on other sites More sharing options...
requinix Posted December 30, 2010 Share Posted December 30, 2010 Still wondering what that error number is... Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152816 Share on other sites More sharing options...
QuickOldCar Posted December 30, 2010 Share Posted December 30, 2010 Shouldn't you use the || operator(OR) not the and if ($extension != "jpg" || $extension != "jpeg" || $extension != "png" || $extension != "gif") { also, where does $existingimage get it's value from? Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152878 Share on other sites More sharing options...
QuickOldCar Posted December 30, 2010 Share Posted December 30, 2010 You can also do like this. $file_type = $_FILES['image']["type"]; if ($file_type != "image/jpg" || $file_type != "image/jpeg" || $file_type != "image/pjpg" || $file_type != "image/gif" || $file_type != "image/png" || $file_type != "image/bmp" || $file_type != "image/x-icon") { echo "$file_type not allowed "; exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152884 Share on other sites More sharing options...
merylvingien Posted December 30, 2010 Author Share Posted December 30, 2010 You can also do like this. $file_type = $_FILES['image']["type"]; if ($file_type != "image/jpg" || $file_type != "image/jpeg" || $file_type != "image/pjpg" || $file_type != "image/gif" || $file_type != "image/png" || $file_type != "image/bmp" || $file_type != "image/x-icon") { echo "$file_type not allowed "; exit(); } Thanks for that, i tried it, but when trying to upload a jpg image its still says not allowed. It wont even give me the jpg extension on the front of the error. requinix not sure how to impliment your answer. Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152940 Share on other sites More sharing options...
the182guy Posted December 30, 2010 Share Posted December 30, 2010 At the top, under the if($_POST) line, add echo '<pre>', print_r($_FILES, true) . '</pre>'; exit(); That will print out the files details, as others have said check the ['error'] property.... Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1152957 Share on other sites More sharing options...
merylvingien Posted December 31, 2010 Author Share Posted December 31, 2010 Thanks for the reply, ok the result from that is: Array ( [photo] => Array ( [name] => _MG_4145.jpg [type] => image/jpeg [tmp_name] => /tmp/phpUwG3fN [error] => 0 => 27055 ) ) It all looks ok to me... Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1153304 Share on other sites More sharing options...
the182guy Posted December 31, 2010 Share Posted December 31, 2010 Now I can see the problem, you're referencing $_FILES['image'] which is wrong, your file input is called 'photo' so it should be $_FILES['photo'], like $image =$_FILES["photo"]["name"]; Change all references from "image" to "photo". Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1153311 Share on other sites More sharing options...
merylvingien Posted December 31, 2010 Author Share Posted December 31, 2010 Thanks the182guy, that was the problem, Quote Link to comment https://forums.phpfreaks.com/topic/222944-image-extension-problem/#findComment-1153325 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.