emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 Please has anyone a solution? Love to get this sorted. ??? Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369143 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 .... You never check if the file exists... You have the functions in there, but you never use them... 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); } That never checks if the file exists.... Something like the following should work: 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); if(file_exists($path . $file_name) || !valid_ext($file_name)) { echo "The file {$file_name} already exists."; } else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } } Also, copy is only set to hold the value of the last move, so if the last one moves correctly and the first 8 don't then the if($copy) idea is defeated entirely. I did this and had a play around and it works a little, Tells me if the file exists and i get an error i have echo'd, but if the file doesnt exist in continues on with readind th code and i get this error. Fatal error: Call to undefined function valid_ext() in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\add_test.php on line 38 Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369201 Share on other sites More sharing options...
MadTechie Posted October 14, 2007 Share Posted October 14, 2007 change if(file_exists($path . $file_name) || !valid_ext($file_name)) { to if(file_exists($path . $file_name) ) { *note, i haven't read the whole thread Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369210 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 change if(file_exists($path . $file_name) || !valid_ext($file_name)) { to if(file_exists($path . $file_name) ) { *note, i haven't read the whole thread Awesome ;D Thats my check if file exists problem fixed. Now file size and type. I have this as a image size filter. //filter by size, function valid_size() { if($_FILES['uploadFile'. $x]['name']['size'] > 1048576) return FALSE; //Over one mega else return TRUE; } does not work, any ideas? Thanks 4 ur help Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369216 Share on other sites More sharing options...
MadTechie Posted October 14, 2007 Share Posted October 14, 2007 try this sample script <?php //test $Name = $_FILES['testfile']['name']; $Size = $_FILES['testfile']['size']; //Test Check if(valid_ext($Name)) { echo "valid ext"; }else{ echo "invalid"; } echo "<br>"; if(valid_size($Size)) { echo "valid size"; }else{ echo "invalid"; } //***FUNCTIONS //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","jpe","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } //filter by size, function valid_size($size) { return ($size <= 1048576); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369220 Share on other sites More sharing options...
emediastudios Posted October 15, 2007 Author Share Posted October 15, 2007 Thanks for that, i added your code but it echo,s file extension invalid when it is and echo,s valid size when it isnt ??? What would be the best way to implement your code in mine? Here is mine <?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']; $agentmobile=$_POST['agentmobile']; $agentemail=$_POST['agentemail']; $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", "5050888202") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; // Uploads Images $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); if(file_exists($path . $file_name) ) { echo "The file {$file_name} already exists."; } else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } } // check if successfully copied if($copy){ print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; } 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 ?> I changed your code to look like this, is this right? <?php //test $Name = $_FILES['uploadFile'. $x]['name']; $Size = $_FILES['uploadFile'. $x]['size']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369797 Share on other sites More sharing options...
MadTechie Posted October 15, 2007 Share Posted October 15, 2007 try this <?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']; $agentmobile=$_POST['agentmobile']; $agentemail=$_POST['agentemail']; $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", "5050888202") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; // Uploads Images $uploadNeed = $_POST['uploadNeed']; // start for loop for($x=0;$x<$uploadNeed;$x++){ $file_name = $_FILES['uploadFile'. $x]['name']; //test $Size = $_FILES['uploadFile'. $x]['size']; //Test Check if(valid_ext($file_name)) { echo "valid ext"; }else{ echo "invalid"; } echo "<br>"; if(valid_size($Size)) { echo "valid size"; }else{ echo "invalid"; } // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($path . $file_name) ) { echo "The file {$file_name} already exists."; } else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } } // check if successfully copied if($copy){ print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; } 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 //***FUNCTIONS //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","jpe","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } //filter by size, function valid_size($size) { return ($size <= 1048576); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-369872 Share on other sites More sharing options...
emediastudios Posted October 17, 2007 Author Share Posted October 17, 2007 Your a champ super Guru. Works!!! Ive been on this for weeks. Only one thing, it still copies the file if it is of invalid ext but is of valid size. It echo's correctly and reconizes the file if is valid or invalid in all categories, size, ext, and file exsists. Can i get the code to stop processing the rest of the code when returns invalid? Like ( if invalid ext or invalid size print url (error.php) and stop proceessing rest of code?) Thanks for your help, i am stoked!! Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371184 Share on other sites More sharing options...
MadTechie Posted October 17, 2007 Share Posted October 17, 2007 try this (quick fix) <?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']; $agentmobile=$_POST['agentmobile']; $agentemail=$_POST['agentemail']; $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", "5050888202") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; // Uploads Images $uploadNeed = $_POST['uploadNeed']; // start for loop for($x=0;$x<$uploadNeed;$x++) { $file_name = $_FILES['uploadFile'. $x]['name']; //test $Size = $_FILES['uploadFile'. $x]['size']; //Test Check $Valid = false; if(valid_ext($file_name)) { echo "valid ext"; $Valid = true; }else{ echo "invalid Type"; } echo "<br>"; if(valid_size($Size)) { echo "valid size"; }else{ $Valid = false; echo "invalid Size"; } if($Valid) { // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($path . $file_name) ) { echo "The file {$file_name} already exists."; }else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } } } // check if successfully copied if($copy) { print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; }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 //***FUNCTIONS //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","jpe","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } //filter by size, function valid_size($size) { return ($size <= 1048576); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371190 Share on other sites More sharing options...
emediastudios Posted October 17, 2007 Author Share Posted October 17, 2007 You live up to your name "Super Guru" Absolute Genuis. You have solved my problem(s) Just one last thing, dont mean to ask to much But on the bottom of my code where it says "Please go back and try agian" How can i make that an active link to "property_add.php. Thanks for everything, cant say how much you have helped. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371214 Share on other sites More sharing options...
MadTechie Posted October 17, 2007 Share Posted October 17, 2007 added an error reporting thing.. *untested <?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']; $agentmobile=$_POST['agentmobile']; $agentemail=$_POST['agentemail']; $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", "5050888202") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; // Uploads Images $uploadNeed = $_POST['uploadNeed']; // start for loop $errors = array(); for($x=0;$x<$uploadNeed;$x++) { $file_name = $_FILES['uploadFile'. $x]['name']; //test $Size = $_FILES['uploadFile'. $x]['size']; //Test Check $Valid = false; if(valid_ext($file_name)) { $Valid = true; }else{ $errors[] = "$file_name has an Invalid FileType"; } echo "<br>"; if(!valid_size($Size)) { $Valid = false; $errors[] = "$file_name is too large"; } if($Valid) { // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($path . $file_name) ) { $errors[] = "The file {$file_name} already exists."; }else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } } } // check if successfully copied if($copy) { print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; } if(count($errors)>0) { echo "The following File(s) could not be uploaded,<BR>"; foreach($errors as $err) { "$err <br>"; } echo "The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif!<br /> <br /> Please go <a href=\"property_add.php\">back</a> and try again"; } // end of loop //***FUNCTIONS //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","jpe","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } //filter by size, function valid_size($size) { return ($size <= 1048576); } ?> EDIT: OOOOPS forgot the s on errors EDIT: also i have never heard of a jpe file!! Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371226 Share on other sites More sharing options...
emediastudios Posted October 17, 2007 Author Share Posted October 17, 2007 Like a charm. ;) Thanks man, your going to be a weathy man one day. Your very smart. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371236 Share on other sites More sharing options...
MadTechie Posted October 17, 2007 Share Posted October 17, 2007 thanx.. and i hope so.. happy PHPing Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371241 Share on other sites More sharing options...
emediastudios Posted October 17, 2007 Author Share Posted October 17, 2007 cant find the missing (s) 0n errors. I will have upcoming payed PHP sites to make in the future, May need some help, would like to rely on you, Will pay you of course. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371251 Share on other sites More sharing options...
MadTechie Posted October 17, 2007 Share Posted October 17, 2007 i corrected the S's i added the edit incase you tried it before the correction.. sounds cool, PM me when you have a more details.. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371260 Share on other sites More sharing options...
emediastudios Posted October 17, 2007 Author Share Posted October 17, 2007 With the last code you supplied me, as with the first, if i attempt to upload more than one file, say 4 images and i zip file or non image file, it uploads all the valid files and not the invalid files but still inserts the record into my database, Is there a way that if one file is not of valid ext or valid size that it does not upload anything, and doesnt insert a record? Thanks Super Guru Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371271 Share on other sites More sharing options...
MadTechie Posted October 17, 2007 Share Posted October 17, 2007 ahh very true (if the invalid file is AFTER a valid one) OK simple fix.. Do one or both of the below (each will fix the proble) if($copy) to if($copy && $Valid) and/or if(file_exists($path . $file_name) ) { echo "The file {$file_name} already exists."; to if(file_exists($path . $file_name) ) { echo "The file {$file_name} already exists."; $copy= false; Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/page/2/#findComment-371525 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.