php - 上传文件表单 - 无法识别 $_FILES 变量

标签 php html

我有以下表格:

<form method="post" action="index.php">
    product name:
    <input type="text" name="product_name" value="<?php echo $product_name;?>"/>
    <br /> <br />
    product details
    <textarea rows = "6" cols = "30" name="product_details" > <?php echo $product_details;?></textarea>
    <br /> <br />
    product price
    <input type="text" name = "product_price" value="<?php echo $product_price;?>"/>
    <br /> <br />
    CN:
    <input type="text" name = "product_cn" value="<?php echo $product_cn;?>"/>
    <br /> <br />
    image
    <input type="file" name="fileField" />
    <br /> <br />

    <input type="submit" name="submit" value="register product" />
</form>

我的问题是,每当我尝试使用此代码处理图像时:

move_uploaded_file($_FILES['fileField']['tmp_name'], "../product_images/$newname");

我收到以下错误:

Notice: Undefined index: fileField

这是为什么呢?

提前致谢!

最佳答案

你必须在表单中添加enctype='multipart/form-data'
Quote from this topic about this

When you make a POST request, you have to encode the data that forms the body of the request in some way.

HTML forms provide two methods of encoding. The default is application/x-www-form-urlencoded, which is more or less the same as a query string on the end of the URL. The other, multipart/form-data, is a more complicated encoding but one which allows entire files to be included in the data.

关于php - 上传文件表单 - 无法识别 $_FILES 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18109802/

相关文章:

html - 为什么这个包装不像预期的那样?

javascript - 使用JQuery/javascript创建动态图像: what am I doing wrong?

php - 在一台服务器上结合静态 HTML、Django 后端和 PHP 论坛?

php - 使用 PHP5 Curl 调用 Facebook Graph API。不同的服务器得到不同的响应

php - 重启 Apache 有风险吗?

php - sql中如何划分学生

html - 如何让 html 优先于 firefox 的选择元素内存?

javascript - 一旦 Javascript 中任何 HTML 元素的值发生变化,就更新 Canvas

php - 如何在 yii 中获取表单元素到 Controller 中

php - 传递从 mysql 表收集的下拉数据