DontTurnAround DontTurnAround - 3 months ago 10
PHP Question

Uploading file via html form. In PHP, $_FILES is empty

So I have this code:

<form name="form" action="./" method="post">
<input type="file" name="newfile" id="newfile" />
</form>


When I run this php script on the action page, nothing happens

echo "$_FILES['newfile']['name'];
print_r($_FILES);


It is like the
$_FILES
variable is empty. Is there any way to fix this?

Answer

You need to set the enctype="multipart/form-data" attribute on your form.

Example 1 from php.net:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
Comments