21

I have a form where I'm posting different fields and every type of field posted seems to work except the input File type.

I'm using var_dump($_POST); and all the other fields are there but nothing in the input type file.

My form part looks like this:

<form enctype="multipart/form-data" id="ajax-form" action="index2.php" method="POST" data-ajax="true">

and works well for everything else.

If there anything that's different in the input type file?

<input type="text" id="myid" name="myid" value="" /> ..This posts value

<input id="theimage" name="theimage" type="file" /> .. does not post value

Any ideas anyone?

1
  • 5
    +1 Because seriously, this question hasn't been asked yet? I remember getting incredibly frustrated the first time I had to figure it out. Aug 16, 2011 at 23:51

2 Answers 2

41

Files are stored in $_FILES, not $_POST

http://php.net/manual/en/reserved.variables.files.php $_FILES variable

http://www.php.net/manual/en/features.file-upload.php Manual on PHP File Uploads.

To handle the file (no error checking):

$ROOT = "/path/to/store/files";
foreach($_FILES as $file => $details)
{   // Move each file from its temp directory to $ROOT
    $temp = $details['tmp_name'];
    $target = $details['name'];
    move_uploaded_file($temp, $ROOT.'/'.$target);
}

See also http://www.php.net/manual/en/function.move-uploaded-file.php for more examples.

3
  • Just tried: echo @$_FILES["theimage"]; but returns array ... how can I get the filename into a variable?
    – Satch3000
    Aug 16, 2011 at 23:54
  • If you var_dump the array, you'll see it's laid out with several entries, one for each file, then several pieces of data about the file. I'll edit my post with an example. Aug 16, 2011 at 23:58
  • php.net/manual/en/features.file-upload.post-method.php details the layout of the $_FILES array. Aug 17, 2011 at 0:03
0

Actually if you try Firefox (>13), you can get the posted value. But if you are using Chrome or Safari, you can't get the posted value. I think the it is related with the Browsers.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.