Page 1 of 1

upload function not working properly

Posted: Fri Jan 30, 2009 7:57 am
by bluebirdjc
i am trying to uplaod a file but i get the following errors

Notice: Undefined index: uploadFile in /nfs/mntI4/projectsite/J.K.Childs/file1.php on line 8

Notice: Undefined index: uploadFile in /nfs/mntI4/projectsite/J.K.Childs/file1.php on line 9

although i can see the lines where the errors should be i cannot understand what they are. can anyone spot what is wrong with this code

here is the code from my website

<form action="file1.php" method="post">
Document to upload (please only upload microsoft word documents others will not work): <input type="file" name="upload">
<input type="submit" name="upload2" value="uploadFile">
</form>

here is the code from my php form

<html>
<head>
<title>Php document for uploading files</title>
</head>
<body>
<?php

move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
"../uploads/{$_FILES['uploadFile'] ['name']}")

?>
</body>
</html>

can anyone spot what might be wrong

Re: upload function not working properly

Posted: Fri Jan 30, 2009 8:34 am
by shiznatix
when you run that page, it automatically is trying to upload the file because you have the function move_uploaded_file called at the end of it. The Notices come from the fact that you are trying access an array element that does not exist. In easier terms:

You open that page, it goes though, top to bottom, and reaches the "move_uploaded_file" function. Your code:

Code: Select all

move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "../uploads/{$_FILES['uploadFile'] ['name']}")
says "take the variable $_FILES['uploadFile']['tmp_name'] and put it in the uploads folder and save it as $_FILES['uploadFile']['name'] (another variable name)"
Well, you see, nobody has said what the variables $_FILES['uploadFile']['tmp_name'] and $_FILES['uploadFile']['name'] are! If you have not submitted a form with them in it then PHP won't do it automagically and you did not set them anywhere in your script so they don't exist!

And what is why you get your error. take a look at the functions isset() and empty() in the manual for ways around this delema you are finding yourself in.