phpretard Posted September 4, 2008 Share Posted September 4, 2008 Can some help me modify this to accept only .jpg or .gif <input name='uploaded' type='file' size='50' DISABLED /> // FORM FIELD function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['uploaded']['name']) ; $ran = $_SESSION['MemberID']; $ran2 = $ran."."; $target = "banners/logos/"; $target = $target . $ran2.$ext; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { $E="UPLOADED"; } else { $E="ERROR UPLOADING"; } Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted September 4, 2008 Share Posted September 4, 2008 Well, that's quite easy...just use an if statement to check value of the $exts variable.... Quote Link to comment Share on other sites More sharing options...
phpretard Posted September 4, 2008 Author Share Posted September 4, 2008 if ($ext=='???'){ PASS } else { NO DICE } I am not sure where to interupt th code either. Like that? Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted September 4, 2008 Share Posted September 4, 2008 Yes, that's spot on... I'm a bit confused....why do you have a function to find your extension? Have you had a look at $_FILES? That has all the functions you need... I would suggest you check the extension first before doing anything else because if it isn't correct you won't need to do anything else right ? Quote Link to comment Share on other sites More sharing options...
aschk Posted September 4, 2008 Share Posted September 4, 2008 You might want to consider the following instead: $accepted_extensions = array('image/jpg','image/gif'); if(in_array($_FILES['uploaded']['type'], $accepted_extensions)){ // Perform upload move file here. } else { // fail... } Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 4, 2008 Share Posted September 4, 2008 I've always done it like this, for some unknown reason. $accepted_extensions = array("jpg","jpeg","png","gif"); $file_extension = explode(".", $_FILES['uploaded']['name']); if(in_array($file_extension[count($file_extension)-1], $accepted_extensions)){ //Perform file upload }else{ //Failed to pass extension check. } That's just me though. 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.