9

This is driving me crazy. I'm trying to figure out how to upload a file. I've got two very simple files, yet it doesn't seem to work. This first is the file that allows the user to choose the file:

<html>
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>

<form action="getfile.php" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload File">
</form>
</body>
</html>
</code>

The second is the php file that handles it:

<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php

print_r($_FILES);
print "<P>\n";

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

?>
</body>
</html>

Since -- except for the print_r -- I pulled these off a website tutorial on how to do a file upload, I'd think these files are okay.

The print_r($FILES) return a completely empty array.

I also checked the php.ini. File uploads are allowed, and the max size is 2M, which I assume is 2 megabytes, which is far larger than the file I've been trying to upload.

What else could be wrong?

Thanks,

Sean.

Jim
  • 22,354
  • 6
  • 52
  • 80
Sean
  • 431
  • 2
  • 6
  • 14

3 Answers3

29

Add the proper enctype attribute to your form tag:

<form action="getfile.php" method="post" enctype="multipart/form-data">

It's documented here: http://www.php.net/manual/en/features.file-upload.post-method.php

Also, make sure there's no space between your brackets when you access multi-dimensional arrays:

$_FILES['uploadFile']['tmp_name']
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • The enctype was exactly the problem. Thank you. If the other hadn't been from a tutorial website and be supposed to work.. sigh. Sean. – Sean Aug 04 '10 at 13:28
  • Glad to help. It's a very common error. If you think this is the correct/best answer, please don't forget to mark it correct using the green check mark. – Scott Saunders Aug 04 '10 at 13:36
  • @Sean - don't worry. This is the classical beginner's mistake when it comes to HTML file uploads. It's one of those things you either know or don't know, intuition is useless here. – Alan Plum Aug 04 '10 at 15:18
1

You shuold use attribute enctype="multipart/form-data" in form tag.

NeDark
  • 1,212
  • 7
  • 23
  • 36
0

Add that in Form tag

enctype="multipart/form-data"

user395702
  • 106
  • 6