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

百般思念 提交于 2019-12-02 04:31:19

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>

If you don't specify <form … enctype="multipart/form-data"> then the data will be encoded in a way that doesn't support files.

Add the attribute with that value.

From the HTML specification:

enctype = content-type [CI]

This attribute specifies the content type used to submit the form to the server (when the value of method is "post"). The default value for this attribute is "application/x-www-form-urlencoded". The value "multipart/form-data" should be used in combination with the INPUT element, type="file".

From the PHP manual:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
OneNerd
echo "$_FILES['newfile']['name'];

That's a syntax error. Should be

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

or

echo "{$_FILES['newfile']['name']}";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!