emediastudios Posted October 5, 2007 Share Posted October 5, 2007 I have this code, it works very well foe me except one thing. I want to filter the the uploaded image files to be either jpeg, jpg, gif or png. Any ideas? Heres the code, i have a image filter in my code but it does not work. <?php //This is the directory where images will be saved $path = '../images/'; //This gets all the other information from the form $name=$_POST['name']; $suburb=$_POST['suburb']; $price=$_POST['price']; $content=$_POST['content']; $content2=$_POST['content2']; $uploadFile0=($_FILES['uploadFile0']['name']); $uploadFile1=($_FILES['uploadFile1']['name']); $uploadFile2=($_FILES['uploadFile2']['name']); $uploadFile3=($_FILES['uploadFile3']['name']); $uploadFile4=($_FILES['uploadFile4']['name']); $uploadFile5=($_FILES['uploadFile5']['name']); $uploadFile6=($_FILES['uploadFile6']['name']); $uploadFile7=($_FILES['uploadFile7']['name']); $uploadFile8=($_FILES['uploadFile8']['name']); // Connects to your Database mysql_connect("localhost", "root", "************") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$uploadFile0', '$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; $uploadNeed = $_POST['uploadNeed']; // start for loop for($x=0;$x<$uploadNeed;$x++){ $file_name = $_FILES['uploadFile'. $x]['name']; // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","jpe","png","gif"); $extension = strtolower(substr(strrchr($file_name,"uploadFile" . $x),1)); if(in_array($extension, $valid)) return TRUE; else return FALSE; } //filter by size, function valid_size() { if($_FILES['uploadFile'. $x]['name']['size'] > 1048576) return FALSE; //Over one mega else return TRUE; } // check if successfully copied if($copy){ print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; } else{ echo "$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br /> <br /> Please go back and try agian"; } // end of loop ?> Link to comment https://forums.phpfreaks.com/topic/71937-image-upload-filter/ Share on other sites More sharing options...
~n[EO]n~ Posted October 5, 2007 Share Posted October 5, 2007 maybe this will help just change $_FILES['newsl_img']['name'] to $_FILES['uploadFile1']['name'] (your file field name) <?php if (empty($_FILES["newsl_img"]["name"])) echo 'Please select a valid image'; if(!((ereg(".jpg", $_FILES['newsl_img']['name'])) || (ereg(".gif", $_FILES['newsl_img']['name'])) || (ereg(".swf", $_FILES['newsl_img']['name'])) || (ereg(".JPG", $_FILES['newsl_img']['name'])) || (ereg(".GIF", $_FILES['newsl_img']['name'])) || (ereg(".SWF",$_FILES['newsl_img']['name'])))) echo 'Please select jpg, gif or swf file only'; ?> Link to comment https://forums.phpfreaks.com/topic/71937-image-upload-filter/#findComment-362324 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.