paddy_fields Posted December 15, 2013 Share Posted December 15, 2013 (edited) Hi, I'm trying to create an upload script for just .doc, .docx, and .pdf files I'm getting an Notice: Undefined index: file in /Users/pat/Sites/recruitment/RecruitSmart/upload/uploader.php on line 7 for each time I use $_FILES ? $path = "CV/".$_FILES['file']['name']; $allowedExts = array("pdf", "doc", "docx"); $extension = end(explode(".", $_FILES["file"]["name"])); if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Error"; } else { copy($_FILES['file']['tmp_name'], $path); echo "Success"; } } else { echo "Wrong file type"; } Edited December 15, 2013 by paddyfields Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/ Share on other sites More sharing options...
requinix Posted December 15, 2013 Share Posted December 15, 2013 If the field named "file"? Have you already submitted the form? By the way, you can't trust the "type". It's provided by the browser which means I could upload any type of file I wanted. Figure out the file type yourself, like with the finfo extension, but know that .docx files look like (they actually are) .zip files. Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462394 Share on other sites More sharing options...
ryanmetzler3 Posted December 15, 2013 Share Posted December 15, 2013 Try this if isset(($_FILES["file"]["error"] > 0)){ echo"Error"} Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462406 Share on other sites More sharing options...
mac_gyver Posted December 15, 2013 Share Posted December 15, 2013 (edited) @ryanmetzler3, the point of programming help is to find what is causing the problem and fix it. the line of code you posted, besides being invalid php, is just slapping a band-aid over the top of the problem. you would use isset() (it requires a variable as a parameter, not an expression) to test for an optional variable, one that may or may not exist during normal program execution. in this case the variable is expected to exist if the form has been submitted (even if no file was selected) and the problem is one of a half-dozen things that could cause the expected index to not exist. Edited December 15, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462407 Share on other sites More sharing options...
paddy_fields Posted December 16, 2013 Author Share Posted December 16, 2013 (edited) By the way, you can't trust the "type". It's provided by the browser which means I could upload any type of file I wanted. Figure out the file type yourself, like with the finfo extension, but know that .docx files look like (they actually are) .zip files. In that case I'll just read up on finfo. There's no point in me using the current method if it is unsafe. Thank you. Edited December 16, 2013 by paddyfields Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462416 Share on other sites More sharing options...
paddy_fields Posted December 16, 2013 Author Share Posted December 16, 2013 I'm going to use finfo to check the MIME of the document, but for now I still can't seem to stop getting 'Undefined Index' errors? cvupload.php <form action="uploader.php" method="post"> <p>File Upload<p> <p>Select file <input name="cv" type="file" size="50" /></p> <input type="submit" value="Upload" /> uploader.php <?php error_reporting(E_ALL); ini_set('display_errors', 1); $target = "CV/"; $target = $target . basename( $_FILES['cv']['name']) ; if(move_uploaded_file($_FILES['cv']['tmp_name'], $target)) { echo "Your CV named ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, your CV could not be uploaded."; } ?> I can't see why? Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462494 Share on other sites More sharing options...
Ch0cu3r Posted December 16, 2013 Share Posted December 16, 2013 For file uploads to work you need to add the enctype="multipart/form-data" attribute to your <form> tag. <form action="uploader.php" method="post" enctype="multipart/form-data"> <p>File Upload<p> <p>Select file <input name="cv" type="file" size="50" /></p> <input type="submit" value="Upload" /> Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462495 Share on other sites More sharing options...
scootstah Posted December 16, 2013 Share Posted December 16, 2013 You need to put enctype="multipart/form-data" in your form tag. Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462496 Share on other sites More sharing options...
paddy_fields Posted December 16, 2013 Author Share Posted December 16, 2013 That's great, thank you both. Quote Link to comment https://forums.phpfreaks.com/topic/284778-_files-as-undefined-index/#findComment-1462497 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.