解析多部分表单数据 [英] Parsing multipart form data

查看:114
本文介绍了解析多部分表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将HTML POST版本的表单放在一起,该表单具有两个字段-文件上载和文本字段.由于表单具有用于文件上传的类型multipart/form-data,因此我无法通过普通的PHP $ _POST变量获取文本字段.那么如何使用PHP获取表单中的文本字段?

I'm trying to put together a HTML POST-ed form that has two fields--a file upload, and a text field. Since the form has a type multipart/form-data for the file upload, I can't get at the text field through the normal PHP $_POST variable. So how can I get at the text field in the form with PHP?

根据要求,下面是一些代码,基本上直接取自安德鲁:

As per requested, here's some code, basically taken directly from Andrew:

<html>
    <body>
        <form action="test2.php" method="post" enctype="multipart/form-data">
            Name: <input type="text" name="imageName" />
            Image: <input type="file" name="image" />
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

<?php
  echo $_POST['imageName'];
  echo "<pre>";
  echo var_dump($_FILES['image']);
  echo "</pre>";
?>

这是整个测试文件.如果删除该enctype,则可以获取POST版本的数据,但是当然不能获取该文件.使用enctype作为multipart/form-data时,我可以获取文件,但是从POST版本获得的数据中什么也没有.

That's the entire test file. If I remove the enctype, I can get the POST-ed data, but not the file of course. With the enctype as multipart/form-data, I can get the file, but nothing from the POST-ed data.

以下是带有enctype的输出:

Here's the output with the enctype:

array(5) {
  ["name"]=>
  string(34) "testing.png"
  ["type"]=>
  string(0) ""
  ["tmp_name"]=>
  string(0) ""
  ["error"]=>
  int(1)
  ["size"]=>
  int(0)
}

没有:

testing

NULL

两次都输入相同的数字.

Same exact input both times.

推荐答案

文件上传通过 $_POST (假设您的HTML表单具有其method属性设置为"post").

File uploads come through $_FILES. Everything else comes through $_POST (assuming of course your HTML form had its method attribute set to "post").

这篇关于解析多部分表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆