DontTurnAround DontTurnAround - 1 year ago 97
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" />

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

echo "$_FILES['newfile']['name'];

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

Answer Source

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

Example 1 from

<!-- 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" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download