iMiles Posted July 13, 2010 Share Posted July 13, 2010 Hey there, I'm making a file upload php page. I needs to make sure the file being uploaded has an allowed extension. Allowed extensions are stored in a array called $allow. I'm trying to use strpos(). Upload.php: <?php if (isset($_POST['upload'])) { // Properties of the file $name = $_FILES["filename"]["name"]; $type = $_FILES["filename"]["type"]; $size = $_FILES["filename"]["size"]; $temp = $_FILES["filename"]["tmp_name"]; $allow = array('.java', '.class'); $error = $_FILES["filename"]["error"]; if ($error > 0) { echo "Error uploading file! Code $error."; } else { if (strpos($name, $allow) == false) { // Of course this won't work, but what will? echo "File type not allowed!"; } else { move_uploaded_file($temp, "games/" . $name); echo "Successfully uploaded game."; } } } ?> <p> <html> <p> <form action = 'upload.php' method = 'POST' enctype = 'multipart/form-data'> <input type = 'file' name = 'filename'> <p> <input type = 'submit' name = 'upload' value = 'Upload'> </form> </html> Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/207559-strpos-array-problem/ Share on other sites More sharing options...
kenrbnsn Posted July 13, 2010 Share Posted July 13, 2010 You can use the pathinfo function: <?php $allow = array('java', 'class'); if (!in_array(pathinfo($name,PATHINFO_EXTENSION),$allow)) { echo "File type not allowed!"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/207559-strpos-array-problem/#findComment-1085179 Share on other sites More sharing options...
iMiles Posted July 13, 2010 Author Share Posted July 13, 2010 Thanks! Work's just fine now. Link to comment https://forums.phpfreaks.com/topic/207559-strpos-array-problem/#findComment-1085181 Share on other sites More sharing options...
Pikachu2000 Posted July 13, 2010 Share Posted July 13, 2010 EDIT: D'oh! I see Ken beat me, but I'll throw this out there since I already typed it out and tested it . . . $_FILES['filename']['name'] = 'damn.jpg'; $name = array(); $name = explode( '.', $_FILES['filename']['name'] ); $ext = array_pop($name); if( $ext == 'java' || $ext == 'class' ) { // extension meets specs } else { //extension doesn't meet specs. } Link to comment https://forums.phpfreaks.com/topic/207559-strpos-array-problem/#findComment-1085183 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.