简体   繁体   English

通过html表单上传文件。 在PHP中,$ _ FILES为空

[英]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 当我在操作页面上运行此php脚本时,没有任何反应

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

It is like the $_FILES variable is empty. 就像$_FILES变量为空。 Is there any way to fix this? 有没有什么办法解决这一问题?

You need to set the enctype="multipart/form-data" attribute on your form. 您需要在enctype="multipart/form-data"上设置enctype="multipart/form-data"属性。

Example 1 from php.net : 来自php.net的示例1

<!-- 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. 如果您未指定<form … enctype="multipart/form-data">那么数据将以不支持文件的方式进行编码。

Add the attribute with that value. 用该值添加属性。

From the HTML specification : 根据HTML规范

enctype = content-type [CI] enctype =内容类型[CI]

This attribute specifies the content type used to submit the form to the server (when the value of method is "post"). 此属性指定用于向服务器提交表单的内容类型(当method的值为“ post”时)。 The default value for this attribute is "application/x-www-form-urlencoded". 此属性的默认值为“ application / x-www-form-urlencoded”。 The value "multipart/form-data" should be used in combination with the INPUT element, type="file". 值“ multipart / form-data”应与INPUT元素type =“ file”结合使用。

From the PHP manual : PHP手册

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

That's a syntax error. 那是语法错误。 Should be 应该

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM