PHP - 解析多部分表单数据 [英] PHP--parsing multipart form data
问题描述
我试图将HTML POST-ed表单放在一起,它有两个字段 - 文件上传和文本字段。由于表单的文件上传类型为multipart / form-data,因此无法通过正常的PHP $ _POST变量获取文本字段。那么我怎样才能在PHP的表单中的文本字段?
按照要求,这里有一些代码,基本上直接从安德鲁:
< html>
< body>
< form action =test2.phpmethod =postenctype =multipart / form-data>
名称:< input type =textname =imageName/>
图片:< input type =filename =image/>
< input type =submitvalue =submit/>
< / form>
< / body>
< / html>
<?php
echo $ _POST ['imageName'];
echo< pre>;
echo var_dump($ _ FILES ['image']);
回声< / pre>;
?>
这是整个测试文件。如果我删除了enctype,我可以得到POST-ed数据,但不是当然的文件。使用enctype作为multipart / form-data,我可以获取文件,但是没有任何来自POST-ed数据。
以下是enctype的输出:
array(5){
[name] =>
string(34)testing.png
[type] =>
string(0)
[tmp_name] =>
string(0)
[error] =>
int(1)
[size] =>
int(0)
}
没有:
测试
NULL
两次输入相同的确切输入。
文件上传通过其他一切都通过 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: 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. Here's the output with the enctype: Without: Same exact input both times. File uploads come through 这篇关于PHP - 解析多部分表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! $ _ POST
(当然假设你的HTML表单的方法属性设置为
post
)。 p> <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>";
?>
array(5) {
["name"]=>
string(34) "testing.png"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
testing
NULL
$_FILES
. Everything else comes through $_POST
(assuming of course your HTML form had its method
attribute set to "post"
).