slim_jimmy7 Posted April 5, 2010 Share Posted April 5, 2010 Wondering how I would go about trying to allow gif, and png files in addition to jpeg files in this instance? Thank you for your help! //Check that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is legit $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg")) { //Determine the path to which we want to save this file $newname = '../include/images/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], dirname(__FILE__).'/images/'.$filename))) { echo "It's done! The file has been saved as: ".$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only .stn files or N2003 files under 350Kb are accepted for upload"; } } else { echo "No file uploaded!"; } Link to comment https://forums.phpfreaks.com/topic/197636-quick-question-on-file-upload/ Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 Instead of using if (($ext == "jpg")) { Create an array of allowed extensions then use in_array() to check for a match, if no match then the file is not allowed. $allowed = array('gif', 'jpg', 'jpeg', 'png'); if(in_array(strtolower($ext), $allowed)) { // file is allowed } else { // file not allowed } Also, you're using strpos() to find the . in the filename, but strpos will return the index of the first occurence, so your code won't work with filenames that have multiple . characters in like beach.holiday.jpg You can use strrpos() instead, which will return the index of the last occurence, which will mean the code works on filenames with more than one period character. Link to comment https://forums.phpfreaks.com/topic/197636-quick-question-on-file-upload/#findComment-1037251 Share on other sites More sharing options...
slim_jimmy7 Posted April 5, 2010 Author Share Posted April 5, 2010 Thank you very much for your help! I will try that when I get home! Link to comment https://forums.phpfreaks.com/topic/197636-quick-question-on-file-upload/#findComment-1037261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.