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"; } Link to comment https://forums.phpfreaks.com/topic/122677-file-upload/ 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.... Link to comment https://forums.phpfreaks.com/topic/122677-file-upload/#findComment-633463 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? Link to comment https://forums.phpfreaks.com/topic/122677-file-upload/#findComment-633465 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 ? Link to comment https://forums.phpfreaks.com/topic/122677-file-upload/#findComment-633469 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... } Link to comment https://forums.phpfreaks.com/topic/122677-file-upload/#findComment-633498 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. Link to comment https://forums.phpfreaks.com/topic/122677-file-upload/#findComment-633518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.