emediastudios Posted October 14, 2007 Share Posted October 14, 2007 I have this code. The image size filter works but not the file extension filter. Would like a check to see if file exsists too. Thanks for any help //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; } Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/ Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 if($img = @GetImageSize('URL OF IMAGE')) { echo 'image exists'; } else { echo 'image does not exist'; } That should be satisfactory enough, I believe fopen needs to be enabled. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368904 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 thanks, your a wizard, but how would include that code in mine, code below <?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()) ; $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\">"; //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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368915 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 I want to filter image size, type and check if file name already exists. Thans heaps for your help, may have this finished today after all, your the best, cant thank you enough Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368918 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 Oh, to check to see if the filename exists, very easy :-D We all love php's latest glob() function. function existingfile($filename){ $files = glob("*.$extension"); if(in_array($filename, $files)) return TRUE; else return FALSE; } Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368925 Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 kratsg that's a lot of pointless processing... if(file_exists($filename)) echo 'file exists'; Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368927 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 i added the code, it still uploads the files, Does it check in my ../images folder? Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368931 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 Actually corbin, the file_exists function doesn't work for images all the time. I've had the problem on a gallery script I made. I uploaded an image, use a file_exists on it, and it said it didn't exist... So it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368933 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 i added the code, it still uploads the files, Does it check in my ../images folder? Change from: $files = glob("*.$extension"); To: $files = glob("../images/*.$extension"); Basically, make the ../images/ work based on where the script is running. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368934 Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 It checks if a file exists.... It doens't know if that file is an image or if its a text document. No offense, and I could be wrong of course, but I think something else in that script was causing that problem. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368936 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 See what you mean, all the files are going to be image files, jpeg, jpg, gif or png. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368937 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 Another reason I use GetImageSize and not file_exists is because file_exists only checks on your server, but GetImageSize works for both absolute and relative urls. And also, she wasn't checking to see if the file itself existed (first script), but rather if the filename existed (second script) Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368938 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 I added this to my code //filter file exists function existingfile($filename){ files = glob("../images/*.$extension"); /////////////// line 46 if(in_array($filename, $files)) return TRUE; else return FALSE; } and i get this error Parse error: syntax error, unexpected '=' in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\add_test.php on line 46 Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368939 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 Please help, am so confused now ??? Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368943 Share on other sites More sharing options...
pocobueno1388 Posted October 14, 2007 Share Posted October 14, 2007 You forgot the '$' on this line files = glob("../images/*.$extension"); Change to $files = glob("../images/*.$extension"); Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368944 Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 //filter file exists function existingfile($filename){ files = glob("../images/*.".$extension); /////////////// line 46 if(in_array($filename, $files)) return TRUE; else return FALSE; } Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368945 Share on other sites More sharing options...
pocobueno1388 Posted October 14, 2007 Share Posted October 14, 2007 //filter file exists function existingfile($filename){ files = glob("../images/*.".$extension); /////////////// line 46 if(in_array($filename, $files)) return TRUE; else return FALSE; } Forgot the dollar sign again Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368946 Share on other sites More sharing options...
corbin Posted October 14, 2007 Share Posted October 14, 2007 Another reason I use GetImageSize and not file_exists is because file_exists only checks on your server, but GetImageSize works for both absolute and relative urls. And also, she wasn't checking to see if the file itself existed (first script), but rather if the filename existed (second script) Ahhh normally I dont like to argue on PHPFreaks since I don't think forums are a good place for arguements, but I'm in the right mood so here it comes: I have this code. The image size filter works but not the file extension filter. Would like a check to see if file exsists too. Thanks for any help There is the thread starter asking how to see if a file exists.... That glob example will simply build an array of filenames in a folder, yes? That means that if a file exists in the folder then the file will be added to the array. If you didn't get what I just said, read it until it hits you. With file_exists you can have the same problem, but due to the extra processing time involved with the glob thing, you're more likely to come across the problem of the file being created inbewteen checking if it doesn't exists and creating it. I've never used ImageGetSize, but I would assume that unless you plan on using the extra data it provides, there is no advantage from using it over file_exists. Another reason I use GetImageSize and not file_exists is because file_exists only checks on your server, but GetImageSize works for both absolute and relative urls. I would assume you mean absolute and relative filepaths, not URLs since there is no reason for the thread creator to be entering web addresses in his/her script. file_exists('../lol.txt') and file_exists('/some/folder/lol.txt') are both valid. A relative and absolute path ;p. Bleh well I'm done now. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368955 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 I added the code and made the changes, thanks everyone, i dont get errors now but the image still uploads and overwrites the old one. Here is my full code <?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()) ; $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 file exists function existingfile($filename){ $files = glob("../images/*.".$extension); /////////////// line 46 if(in_array($filename, $files)) return TRUE; else return FALSE; } //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\">"; //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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368959 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 Does the code need to be structured in a different order, cant seem to see what is wrong as i have no errors, just doesnt do exactly what i want. Thanks again for everyones help Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368964 Share on other sites More sharing options...
corbin Posted October 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-368966 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 I added your code <?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()) ; 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); } } // 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 ?> But now it doesnt post the files or record and i get this error The File(s) could not be uploaded! The file must be under 1 meg and be of a valid extension type, (jpeg, ,jpe, jpg, png or gif! Please go back and try agian Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-369046 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 I have gone all day on this one and it is driving me crazy>..Please someone. My Code <?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()) ; $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\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$ID','$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 ?> From all the responses proir to this, im sure there is an answer in there somewhere, i'm a begginer and still learning. Could some one paste the nessessary code in mine and ill see if it works. Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-369091 Share on other sites More sharing options...
monkeybidz Posted October 14, 2007 Share Posted October 14, 2007 When checking image size, do you really need the space after '.' and before $x ? $_FILES['uploadFile'. $x] Looks like it will not join the two variables if separate. Just wondering. Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-369122 Share on other sites More sharing options...
emediastudios Posted October 14, 2007 Author Share Posted October 14, 2007 Thats the only filter that works at this stage. The image size filter. image type, and check if file exists doesnt :'( Quote Link to comment https://forums.phpfreaks.com/topic/73149-solved-image-filter/#findComment-369139 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.