views:

3040

answers:

4

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.

+3  A: 

File uploads come through $_FILES. Everything else comes through $_POST (assuming your form action was 'Post').

Jonathan Sampson
A: 

$_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
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
should work. your problem is somewhere else.
Javier
What version of PHP are you using? Any configuration changes and special extensions enabled?
Andrew Moore
A: 

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

lxsg
A: 

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

Greg Willson