SkyRanger Posted March 5, 2013 Share Posted March 5, 2013 I keep receiving the error Invalid file when uploading this is my form <form method="post" action="upload_imfile.php" enctype="multipart/form-data"> <label>File to Upload:</label> <input type="file" name="file"> <input type="submit" name="submit" value="Proceed with Upload"> </form> $allowedExts = array("pdf", "zip", "rar"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/zip") || ($_FILES["file"]["type"] == "application/x-zip-compressed") || ($_FILES["file"]["type"] == "multipart/x-zip") || ($_FILES["file"]["type"] == "application/x-compressed") || ($_FILES["file"]["type"] == "application/octet-stream")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("clients/$client/personal/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "clients/$client/personal/" . $_FILES["file"]["name"]); echo "Stored in: " . "clients/$client/personal/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/ Share on other sites More sharing options...
AyKay47 Posted March 5, 2013 Share Posted March 5, 2013 You don't need to wrap each condition in parens. Unless I am missing something, I don't see anything obvious that stands out in the if condition. What debugging steps have you taken so far? Be sure to validate each conditions' value. Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416774 Share on other sites More sharing options...
SkyRanger Posted March 5, 2013 Author Share Posted March 5, 2013 I have tried uploading the 3 file types pdf zip and rar with all the same result. File is getting passed to the script but seem to fail at the validator. Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416780 Share on other sites More sharing options...
AyKay47 Posted March 5, 2013 Share Posted March 5, 2013 What debugging steps have you taken so far? Be sure to validate each conditions' value. Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416783 Share on other sites More sharing options...
Barand Posted March 5, 2013 Share Posted March 5, 2013 (edited) put error_reporting(-1); at top of code. You may need $allowedExts = array("pdf", "zip", "rar"); $arr = explode(".", $_FILES["file"]["name"]); $extension = end($arr); Edited March 5, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416788 Share on other sites More sharing options...
DavidAM Posted March 5, 2013 Share Posted March 5, 2013 You don't need to wrap each condition in parens.There's nothing wrong with wrapping each condition in parens, as it clearly documents the programmers intentions. I wrap ALL of my conditions in parens, in essence telling the interpreter/compiler exactly what I mean, so we don't misunderstand each other. if ($_FILES["file"]["error"] > 0) { This condition should really be tested FIRST! Before using any other elements of the $_FILES array, you need to verify that you actually received a file. If I try to upload a 2G file and your PHP/server fails it because it exceeds the max allowed size, this is the only element of the array that is meaningful. Since your current error message is generic and used if ANY condition fails, it is hard to know why. You may want to printf('<PRE>%s</PRE>', htmlspecialchars(print_r($_FILES, true))); to have a look at the $_FILES array and see why it reached that point (well, at least during development). Or you may want to break up your tests and produce a message telling why the file is invalid. Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416802 Share on other sites More sharing options...
Solution SkyRanger Posted March 5, 2013 Author Solution Share Posted March 5, 2013 Thanks guys, I added and edited what you mentioned and it works perfectly. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416807 Share on other sites More sharing options...
AyKay47 Posted March 5, 2013 Share Posted March 5, 2013 There's nothing wrong with wrapping each condition in parens, as it clearly documents the programmers intentions. I wrap ALL of my conditions in parens, in essence telling the interpreter/compiler exactly what I mean, so we don't misunderstand each other. No there is nothing wrong with it, but wrapping each condition in parens AND writing them each on a new line is overkill. The interpreter is not a person, it does not misunderstand. If that is your style then more power to you but I believe that causes more typing then what is needed. The only instance where you need parens in a condition is when you need to specify the order of precedence. Off topic I know, but you called me out. Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416808 Share on other sites More sharing options...
Barand Posted March 5, 2013 Share Posted March 5, 2013 (edited) A AND B OR C Does that mean (A AND B ) OR C or does it mean A AND (B OR C) The parentheses removes any ambiguity for anyone reading the code edit: and removes any problems with operator precedence Edited March 5, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416810 Share on other sites More sharing options...
AyKay47 Posted March 5, 2013 Share Posted March 5, 2013 (edited) Thank you for that rudimentary lesson Barand. This topic isn't worth any more then one more post as it is menial. What I was trying to point out is that this: if((something == something || something == something) && something == something) is the exact same as: if(((something == something) || (something == something)) && (something == something))) only is much more confusing to look at. parens around a single condition: (something == something) is not needed. To use your example Barand: (A AND B) OR C same as: ((A) AND (B)) OR (C) Edit: What's with the double spacing? Edited March 5, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416816 Share on other sites More sharing options...
Barand Posted March 5, 2013 Share Posted March 5, 2013 Thank you for that rudimentary lesson Barand. You're welcome Quote Link to comment https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416817 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.