问题
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>";
?>
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:
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.
回答1:
File uploads come through $_FILES. Everything else comes through $_POST (assuming of course your HTML form had its method attribute set to "post").
回答2:
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.
回答3:
$_POST should work just fine for the text field. You will need to use $_FILES for the actual file. Given the following 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>";
?>
回答4:
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
回答5:
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.
回答6:
[2010-02-13 10:57 UTC] sudeshkmr at 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.
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.
The action="upload.php" <-------- This page has to be different than the file upload form page, and it works on all configurations of PHP!
Do not use for the action parameter.
回答7:
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
来源:https://stackoverflow.com/questions/1075513/parsing-multipart-form-data