darkfreaks Posted August 9, 2011 Share Posted August 9, 2011 can you post lines 52 and 55 so we know Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254847 Share on other sites More sharing options...
TeNDoLLA Posted August 9, 2011 Share Posted August 9, 2011 The errors you are getting is produced probably because the file you uploaded was in your blacklist, which leads to that the file WILL NOT GET MOVED/UPLOADED. And then you try to use the moved image file in your scripts some elsewhere (which does not exist). If I recall correctly from your earlier topics, you are also using a SimpleImage class or something similar. I suggest you try to make things simple and work simple before you dive into someone else's written image classes. You need to understand the basics and the logic flow in the code ALL THE TIME. You just cant get pieces and classes from here and there and put them together and expect everything to work smoothly. Not that the class was very advanced what I looked it day or two ago, but just a note. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254849 Share on other sites More sharing options...
$php_mysql$ Posted August 9, 2011 Author Share Posted August 9, 2011 but 1 thing im not getting is why exit(); does wht i want but if else statement not doing the same? Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254860 Share on other sites More sharing options...
TeNDoLLA Posted August 9, 2011 Share Posted August 9, 2011 but 1 thing im not getting is why exit(); does wht i want but if else statement not doing the same? exit() does not do what you want, or do you want that blank page (then it does)? exit() just ends the script execution in that place. No following script is executed at all. That way you don't have the errors since the script will never run in those parts. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254862 Share on other sites More sharing options...
$php_mysql$ Posted August 9, 2011 Author Share Posted August 9, 2011 ok understand. guess nothing else can be done then with my coding from stopping it to be uploaded if file type and and size are not correct? Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254871 Share on other sites More sharing options...
the182guy Posted August 9, 2011 Share Posted August 9, 2011 Maybe do something like this.. function uploadFile($file, $uploadPath) { $max_size = 5242880; $path = $uploadPath . $file['name']; $blacklist = array(".php", ".jpg", ".jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4", ".html"); $file_type = array("image/gif","image/jpg","image/jpeg","image/png"); $patterns = implode('|', $blacklist); if (preg_match('('. $patterns .')', $file['name'])) { return 'File is blacklisted.'; } else if ($file['size'] > $max_size) { return 'File is too large.'; } else if (!in_array($file['type'], $file_type)) { return 'File type is not allowed.'; } else if (move_uploaded_file($file['tmp_name'], $path)) { return "The file was uploaded successfully."; } else { return "The file was not uploaded successfully. Error code: " . $file['error']; } } I don't like lots of returning like that within a function. It should have as few exit paths as possible, preferably just one, making it easier to read, maintain and debug. I would instead create a return object which has a success (true/false) property and a message property to indicate the reason for failure. Even better would be if the message was a pointer rather than a user friendly message, e.g. TOO_LARGE, where you'd let the front end or view handle the actual turning it into the user friendly message if you needed different languages or want a more MVC style system. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254876 Share on other sites More sharing options...
TeNDoLLA Posted August 9, 2011 Share Posted August 9, 2011 Yeah well, tried to make just a SIMPLE example for this guy. If you see his earlier post, do you think an OO/MVC based approach would have made him understand it more? Agree on that you don't need all the returns, could just use one variable/array to assign the string(s) in it and return the variable at the end of the function. Also I wanted to keep it as close as possible to the original approach he introduced in this thread. I really don't think this thread is about MVC or localization. Ps. What I pasted earlier is not the way I would do it either. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254879 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 i urge people on this forum to no longer help this person...he/she obviously doesn't understand the basics of PHP and doesn't care to.. anyone that has an issue every hour clearly needs to study examples and practice more..as I have stated in another thread of his.. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254883 Share on other sites More sharing options...
$php_mysql$ Posted August 9, 2011 Author Share Posted August 9, 2011 agreed, i asked for help for i could not get it to work if i could i sure would not be here asking for help. thanks Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254887 Share on other sites More sharing options...
the182guy Posted August 9, 2011 Share Posted August 9, 2011 OP, show us all of the relevant code, including the part which is calling your upload function. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254893 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 SEEMS TO ME THAT YOU ARE NOT SPENDING THE TIME TO PROPERLY LEARN AND TEACH YOURSELF PHP...YOUR QUESTIONS ARE BASIC..SOMEONE GIVES YOU A SOLUTION AND YOU DO NOT UNDERSTAND IT AND ASK THEM TO DO IT FOR YOU, THESE ARE NICE PEOPLE ON HERE GIVING YOU THERE TIME FOR FREE WHERE NORMALLY A PROGRAMMER WOULD CHARGE YOU A GOOD CUNK OF MONEY TO HELP YOU...DON'T ABUSE THEIR HELP, IF YOU HAVE A QUESTION ABOUT SOMETHING, CHECK ONLINE RESOURCES BEFORE YOU ASK HERE AND TELL PEOPLE TO HELP YOU..IF YOU DO NOT UNDERSTNAD THE SOLUTIONS HOW CAN YOU EXPECT TO BE ABLE TO PROCEED WITH YOUR CODING... Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254894 Share on other sites More sharing options...
$php_mysql$ Posted August 9, 2011 Author Share Posted August 9, 2011 i definitely appreciate them giving soo much time and help and i in no ways tried to abuse it. i did look around and tried, its nothing like im not trying but since im not as good as they are so i asked them for their help. i do not understand why are you trying to be rude? Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254898 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 because if you look at your other threads.. i am there helping you as well, however when i give you a simple solution, you do not understand it...therefore I end up spending half of the day trying to explain basic code to you...if you look at your threads, the majority of them are like 20+ replies.. you are wasting people time and efforts.. learn the ins and outs of PHP before attempting things like this...look at tutorials, read books etc.. just how I feel about it Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254900 Share on other sites More sharing options...
$php_mysql$ Posted August 9, 2011 Author Share Posted August 9, 2011 so i managed to make the file type and size limit to work function insert($postData) { if(!empty($_FILES['image']["name"])){ $max_size = 5242880; $blacklist = array(".php", ".php.jpg", ".php.jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4", ".html"); $file_type = array("image/gif","image/jpg","image/jpeg","image/png"); if ($_FILES['image']["size"] > $max_size) { echo 'File is too large.'; } else if (preg_match("/$blacklist\$/i", $_FILES['image']['name'])) { echo "Blacklisted File"; } else if (!in_array($_FILES['image']["type"], $file_type)) { echo "file not supported"; }else{ global $uploadPath; $remove_symbols = array('+', '=', '-', '{', '}', '$', '(', ')'); $removed_symbols = str_replace($remove_symbols, "_", $_FILES['image']['name']); $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(225,250); $resize_rename = $uploadPath.$randomnum._.$removed_symbols; $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', name = '".$postData['name']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); echo "ad posted successfully"; } }else{ $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '', name = '".$postData['name']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); echo "ad posted successfully without image"; } } now my issue is the blacklist will not work, what could i be doing wrong that it only display file too long and file not supported error message? Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254953 Share on other sites More sharing options...
darkfreaks Posted August 9, 2011 Share Posted August 9, 2011 you have too many ELSE IF's should be IF ELSE IF ELSE Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254955 Share on other sites More sharing options...
$php_mysql$ Posted August 9, 2011 Author Share Posted August 9, 2011 so i did this function insert($postData) { $max_size = 5242880; $blacklist = array(".php", ".php.jpg", ".php.jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4", ".html"); $file_type = array("image/gif","image/jpg","image/jpeg","image/png"); if(empty($_FILES['image']["name"])){ $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '', name = '".$postData['name']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); echo 'posted without image'; }else if ($_FILES['image']["size"] > $max_size) { echo 'File is too large.'; } else if (preg_match("/$blacklist\$/i", $_FILES['image']['name'])) { echo "Blacklisted File"; } else if (!in_array($_FILES['image']["type"], $file_type)) { echo "file not supported"; }else{ global $uploadPath; $remove_symbols = array('+', '=', '-', '{', '}', '$', '(', ')'); $removed_symbols = str_replace($remove_symbols, "_", $_FILES['image']['name']); $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(225,250); $resize_rename = $uploadPath.$randomnum._.$removed_symbols; $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', name = '".$postData['name']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); echo "ad posted successfully with image"; } } still when i try to upload anything listed in the blacklist array the error message Blacklisted File do not show instead it shows file not supported Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254958 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 you are using an array as the preg_match() first argument.. the first argument needs to be a regular expression pattern, not an array.. that entire condition can be removed, as the !in_array condition takes care of non wanted files.. Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1254971 Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 how could i use a regular expression inside else if? is it possible to use foreach inside else if? Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1255142 Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 nevermind sorted, thanks everyone for your help :-) Quote Link to comment https://forums.phpfreaks.com/topic/244305-if-else-statement-_files-getting-uploaded/page/2/#findComment-1255165 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.