Drongo_III Posted July 4, 2011 Share Posted July 4, 2011 Hi Guys Can someone see where I'm going wrong with this upload script. It's meant to only accept csv files. when i try it out all i get is is the error "invalid file" but i'm uploading a csv file. Upload form <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Upload script <?php if ((($_FILES["file"]["type"] == "text/csv")) && ($_FILES["file"]["size"] < 200000)) { 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("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Any help would be appreciated. Drongo Quote Link to comment https://forums.phpfreaks.com/topic/241067-upload-script/ Share on other sites More sharing options...
TeNDoLLA Posted July 4, 2011 Share Posted July 4, 2011 So the first if is failing, either ($_FILES["file"]["type"] == "text/csv") or ($_FILES["file"]["size"] < 200000) is not matching or both. Try var_dumping these variables to see what they contain to try to figure out why your if clause is failing. Quote Link to comment https://forums.phpfreaks.com/topic/241067-upload-script/#findComment-1238238 Share on other sites More sharing options...
Drongo_III Posted July 4, 2011 Author Share Posted July 4, 2011 Hi TenDoLLA I did as you suggested - didn't know that existed. But the result has left me a little confused. I did : var_dump($_FILES["file"]["type"]); echo "<br/><br/>"; var_dump($_FILES["file"]["size"]); And got the result string(24) "application/vnd.ms-excel" int(50) Which didn't mean a whole lot to me. I would love an explanation of that. Any suggestions based on the var dump? Thanks Drongo So the first if is failing, either ($_FILES["file"]["type"] == "text/csv") or ($_FILES["file"]["size"] < 200000) is not matching or both. Try var_dumping these variables to see what they contain to try to figure out why your if clause is failing. Quote Link to comment https://forums.phpfreaks.com/topic/241067-upload-script/#findComment-1238243 Share on other sites More sharing options...
EdwinPaul Posted July 4, 2011 Share Posted July 4, 2011 It means you have to change if ((($_FILES["file"]["type"] == "text/csv")) to: if ((($_FILES["file"]["type"] == "application/vnd.ms-excel")) Quote Link to comment https://forums.phpfreaks.com/topic/241067-upload-script/#findComment-1238246 Share on other sites More sharing options...
Drongo_III Posted July 4, 2011 Author Share Posted July 4, 2011 Worked a treat! Thank you very much It means you have to change if ((($_FILES["file"]["type"] == "text/csv")) to: if ((($_FILES["file"]["type"] == "application/vnd.ms-excel")) Quote Link to comment https://forums.phpfreaks.com/topic/241067-upload-script/#findComment-1238247 Share on other sites More sharing options...
EdwinPaul Posted July 4, 2011 Share Posted July 4, 2011 From http://www.php.net/manual/en/features.file-upload.post-method.php : You could use the $_FILES['userfile']['type'] variable to throw away any files that didn't match a certain type criteria, but use this only as first of a series of checks, because this value is completely under the control of the client and not checked on the PHP side. Quote Link to comment https://forums.phpfreaks.com/topic/241067-upload-script/#findComment-1238249 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.