26

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.

Kerem
  • 11,377
  • 5
  • 59
  • 58
DashRantic
  • 1,448
  • 4
  • 19
  • 32
  • 1
    As Jonathan and Andrew said, $_POST should work, assuming you used post to post your form (and that is a good assumption, because you have to to post files). So can you post some code? Make sure the name attribute is set in the html text field, and that is what you use to get the text frield value. – alanquillin Jul 02 '09 at 17:01

6 Answers6

36

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

Sampson
  • 265,109
  • 74
  • 539
  • 565
10

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.

shane
  • 101
  • 1
  • 2
  • 1
    The most upvoted answer solves the problem but this can specifically bite you if you are uploading large files. If I'm not mistaken, the length is the sum of all files (see http://stackoverflow.com/questions/10453735/trying-to-upload-video-file-in-php-but-its-too-large). If you are bit by this, the $_FILES['whatever'] will act like an invalid variable. – w0rd-driven Nov 18 '13 at 18:01
5

$_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>";
?>
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
  • 3
    Doesn't work. Just to try, I even copy/pasted what you put there (but added a submit button) and tried that--definitely does not work. If I remove the enctype, it works, but I don't get he file of course, and if I leave it as multipart/form-data, I get the file, but I *don't* get the POST-ed data. – DashRantic Jul 02 '09 at 17:09
  • 1
    What version of PHP are you using? Any configuration changes and special extensions enabled? – Andrew Moore Jul 02 '09 at 18:38
  • @DashRantic, did you find the problem? – David Portabella May 30 '18 at 07:25
1

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

Shanshui
  • 684
  • 1
  • 5
  • 12
  • Try running the code on a test server. The var_dump output would show that both $_POST and $_FILES are filled with the form input and information about the file upload and that they're not mutually exclusive. – Shanshui Jan 11 '12 at 09:01
1

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.

-1

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

  • Cannot confirm that, works great here with send and receiver script sharing the URL. – InI4 Dec 01 '21 at 09:38