Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_FILES empty when trying to do a file upload

Tags:

php

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.

like image 472
Sean Avatar asked Aug 04 '10 13:08

Sean


People also ask

Why can't I upload a file?

Be sure to clear cache/cookies. Close your browser and re-open new windows. Check if you need to install updates to your browser or OS. Restart your computer.

How do you check $_ files is empty?

You can check if there is a value, and if the image is valid by doing the following: if(empty($_FILES['cover_image']['tmp_name']) || ! is_uploaded_file($_FILES['cover_image']['tmp_name'])) { // Handle no image here... }

What does $_ files do in PHP?

Description. $_FILES is a super global variable which can be used to upload files. Here we will see an example in which our php script checks if the form to upload the file is being submitted and generates a message if true. Code of files.

What do you do when a file is not uploaded?

First, please make sure that you are attempting to upload one of the supported file types (see What file types or formats can I upload?). If your file is supported, please try again using a different browser (e.g., Firefox or Chrome instead of Internet Explorer). This should resolve the issue in most cases.


2 Answers

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']
like image 186
Scott Saunders Avatar answered Oct 08 '22 01:10

Scott Saunders


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

like image 45
NeDark Avatar answered Oct 08 '22 02:10

NeDark