lucan Posted March 7, 2011 Share Posted March 7, 2011 I have this code which uploads fine but I am trying to limit the type of files users can upload. For some reason this code still let any file type to be uploaded being very new to php any help would be Appreciated. <?php if ((($_FILES["image"]["type"] == "image/gif") || ($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/pjpeg")) && ($_FILES["image"]["size"] < 20000)) { if ($_FILES["image"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["image"]["name"] . "<br />"; echo "Type: " . $_FILES["image"]["type"] . "<br />"; echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["image"]["tmp_name"]; } } $target = "testimages/"; $target = $target . basename( $_FILES['image']['name']); // $company=$_POST['company']; $image=($_FILES['image']['name']); mysql_connect("localhost", "user", "pass") or die(mysql_error()) ; mysql_select_db("testupload") or die(mysql_error()) ; mysql_query("INSERT INTO `table` (company, image) VALUES ('$company', '$image')") ; if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229921-limit-file-type-help/ Share on other sites More sharing options...
HuggieBear Posted March 7, 2011 Share Posted March 7, 2011 Let me guess, if it's a valid format it displays all the details and uploads the file, and if it's not it doesn't display the data, but still uploads the file? Quote Link to comment https://forums.phpfreaks.com/topic/229921-limit-file-type-help/#findComment-1184258 Share on other sites More sharing options...
lucan Posted March 8, 2011 Author Share Posted March 8, 2011 With this code at the moment it does upload the file correctly. However, i have try a number of ways to restrict the different files with this code and thats when i have the problem you stated "uploads all file but only displays jpg file in the correct formate. Quote Link to comment https://forums.phpfreaks.com/topic/229921-limit-file-type-help/#findComment-1184271 Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 You need an else statement on your very first conditional. If you add an else statement to this that exits the script you'll be fine. The one that says you're not an image or below 20000 bytes. Or just move all the rest of the code into the first if. Quote Link to comment https://forums.phpfreaks.com/topic/229921-limit-file-type-help/#findComment-1184273 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.