php 解析多部分表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1075513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing multipart form data
提问by DashRantic
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?
我正在尝试将一个 HTML POST-ed 表单放在一起,该表单具有两个字段——一个文件上传和一个文本字段。由于表单具有用于文件上传的 multipart/form-data 类型,因此我无法通过普通的 PHP $_POST 变量获取文本字段。那么如何使用 PHP 获取表单中的文本字段呢?
As per requested, here's some code, basically taken directly from Andrew:
根据要求,这里有一些代码,基本上直接取自 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>";
?>
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,我可以获得 POST ed 数据,但当然不能获得文件。将 enctype 设为 multipart/form-data,我可以获得文件,但无法从 POST 数据中获取任何内容。
Here's the output with the enctype:
这是带有 enctype 的输出:
array(5) {
["name"]=>
string(34) "testing.png"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
Without:
没有:
testing
NULL
Same exact input both times.
两次完全相同的输入。
回答by Sampson
回答by shane
Check your post limit. I was going crazy trying to figure out what was causing this. A quick check to Apache error log showed that the post content length exceeded the limit. After raising the limit the post data is available.
检查您的帖子限制。我快疯了,试图找出导致这种情况的原因。对 Apache 错误日志的快速检查显示帖子内容长度超过了限制。提高限制后,发布数据可用。
回答by Andrew Moore
$_POSTshould work just fine for the text field. You will need to use $_FILESfor the actual file. Given the following HTML:
$_POST对于文本字段应该可以正常工作。您将需要$_FILES用于实际文件。鉴于以下 HTML:
<html>
<body>
<form action="self.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>
You can access the fields the following way:
您可以通过以下方式访问字段:
<?php
echo $_POST['imageName'];
echo "<pre>";
echo var_dump($_FILES['image']);
echo "</pre>";
?>
回答by Shanshui
There's nothing wrong with your code; could be an issue with the server configuration.
您的代码没有任何问题;可能是服务器配置的问题。
<form action="" method="post" enctype="multipart/form-data">
Name: <input type="text" name="imageName">
Image: <input type="file" name="image">
<input type="submit" value="submit">
</form>
<?php var_dump($_POST, $_FILES); ?>
Script: http://sandbox.phpieceofcake.com/upload/1246558881125336.php
Source: http://sandbox.phpieceofcake.com/upload/1246558881125336.phps
脚本:http: //sandbox.phpieceofcake.com/upload/1246558881125336.php
来源:http: //sandbox.phpieceofcake.com/upload/1246558881125336.phps
回答by Carlos Vázquez
I had the same problem as shown at the beginning. Try it by loading a file size less than 1MB, if you've been able to upload the file then your problem is the upload_max_filesize and post_max_size values increase those values and try again. This was the solution to my problem.
我遇到了与开头所示相同的问题。尝试加载小于 1MB 的文件,如果您已经能够上传文件,那么您的问题是 upload_max_filesize 和 post_max_size 值会增加这些值并重试。这是我的问题的解决方案。
回答by Greg Willson
[2010-02-13 10:57 UTC] sudeshkmr at yahoo dot com
[2010-02-13 10:57 UTC] sudeshkmr 在 yahoo dot com
I faced the same problem and nothing worked. I have tested on Apache 2.2, PHP 5.3 on Windows Vista. I also tested on Ubuntu (Karmic) with Apache 2.2 and PHP 5.3. I also tested with nGinX 0.8 and PHP 5.3.
我遇到了同样的问题,但没有任何效果。我已经在 Windows Vista 上的 Apache 2.2、PHP 5.3 上进行了测试。我还使用 Apache 2.2 和 PHP 5.3 在 Ubuntu (Karmic) 上进行了测试。我还使用 nGinX 0.8 和 PHP 5.3 进行了测试。
Then I found a workaround. The action="" parameter should not be the script page itself on which you have file upload form. For example, you have a page index.php with the upload form.
然后我找到了一个解决方法。action="" 参数不应是您具有文件上传表单的脚本页面本身。例如,您有一个带有上传表单的页面 index.php。
The action="upload.php" <-------- This page has to be different than the file upload form page, and it works on all configurations of PHP!
action="upload.php" <-------- 这个页面必须不同于文件上传表单页面,它适用于所有PHP配置!
Do not use for the action parameter.
不要用于操作参数。
回答by Rajat Talwar
You can check if your webservices are working fine using chrome rest client extension or uploading multi-part data via this app - http://itunes.apple.com/us/app/rest-client/id503860664?ls=1&mt=8
您可以使用 chrome rest 客户端扩展或通过此应用上传多部分数据来检查您的网络服务是否正常工作 - http://itunes.apple.com/us/app/rest-client/id503860664?ls=1&mt=8

