aebstract Posted March 11, 2009 Share Posted March 11, 2009 The following code is not allowing me to upload any files at all, it returns: Invalid file type. Any help please? <?php session_start(); header("Cache-control: private"); if(!isset($_SESSION["id"])) { header("Location: /login/"); } if(isset($_POST["submit"])) { $file = $_FILES['userfile']; $allowedExtensions = array("avi", "mov", "mp4", "mpg", "mpeg", "wmv"); function isAllowedExtension($fileName) { global $allowedExtensions; return in_array(end(explode(".", $fileName)), $allowedExtensions); } if($file['error'] == UPLOAD_ERR_OK) { if(isAllowedExtension($file['name'])) { if (move_uploaded_file ($_FILES['file']['tmp_name'], "upload/{$_FILES['file']['name']}")) { echo "Has been uploaded"; } else { echo "Not uploaded:<br />"; print "$_FILES[file][error]"; } } else { echo "Invalid file type"; } } else die("Cannot upload"); } if ($var1 == post){ $content .= ' <form action="/videos/post/" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="62914560" /> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Post Video" /> </form> '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149004-only-allow-specific-extensions-to-be-uploaded/ Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 //return in_array(end(explode(".", $fileName)), $allowedExtensions); //break this line to debug.. //is it exploding as expexted? print_r(explode(".", $fileName)); //is it returning extension properly echo end(explode(".", $fileName); //check case as in_array() is case sensitive..you can try to lowercase ur extension before comparing return in_array(end(explode(".", $fileName)), $allowedExtensions); Quote Link to comment https://forums.phpfreaks.com/topic/149004-only-allow-specific-extensions-to-be-uploaded/#findComment-782435 Share on other sites More sharing options...
aebstract Posted March 13, 2009 Author Share Posted March 13, 2009 Okay I threw that in there, sorry it took so long, I was out of work yesterday. Anyways, here is what I have now: <?php session_start(); header("Cache-control: private"); if(!isset($_SESSION["id"])) { header("Location: /login/"); } if(isset($_POST["submit"])) { $file = $_FILES['userfile']; $allowedExtensions = array("avi", "mov", "mp4", "mpg", "mpeg", "wmv"); function isAllowedExtension($fileName) { global $allowedExtensions; return in_array(end(explode(".", $fileName)), $allowedExtensions); print_r(explode(".", $fileName)); echo end(explode(".", $fileName)); return in_array(end(explode(".", $fileName)), $allowedExtensions); } if($file['error'] == UPLOAD_ERR_OK) { if(isAllowedExtension($file['name'])) { if (move_uploaded_file ($_FILES['file']['tmp_name'], "upload/{$_FILES['file']['name']}")) { echo "Has been uploaded"; } else { echo "Not uploaded:<br />"; print "$_FILES[file][error]"; } } else { echo "Invalid file type"; } } else die("Cannot upload"); } if ($var1 == post){ $content .= ' <form action="/videos/post/" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="62914560" /> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Post Video" /> </form> '; } ?> Displaying just one error : Invalid file type Quote Link to comment https://forums.phpfreaks.com/topic/149004-only-allow-specific-extensions-to-be-uploaded/#findComment-783669 Share on other sites More sharing options...
samshel Posted March 13, 2009 Share Posted March 13, 2009 you have to print all those statements before you call return..else they wont be executed. Quote Link to comment https://forums.phpfreaks.com/topic/149004-only-allow-specific-extensions-to-be-uploaded/#findComment-783971 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.