PHP--parsing multipart form data

2019-01-17 21:13发布

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.

7条回答
来,给爷笑一个
2楼-- · 2019-01-17 21:25

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

查看更多
劫难
3楼-- · 2019-01-17 21:29

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.

查看更多
Lonely孤独者°
4楼-- · 2019-01-17 21:31

[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.

查看更多
Anthone
5楼-- · 2019-01-17 21:35

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

查看更多
冷血范
6楼-- · 2019-01-17 21:42

$_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>";
?>
查看更多
老娘就宠你
7楼-- · 2019-01-17 21:44

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.

查看更多
登录 后发表回答