views:

34

answers:

2

I have a form.

In the form, there is an image upload plus a series of checkboxes.

I am trying to loop through the $_POST vars to process them.

When I do

foreach($_POST as $key => $value){echo "$key $value"; }

I only get 'input' $_POST vars outputted. The checkbox values and the image upload value are not.

I am utilizing Code Igniter as a Framework.

Any ideas? Thanks

+1  A: 
  1. Unchecked checkboxes will never be present in POST (by design)
  2. The most obvious reason for files to be missing is that you have not declared the form to send files, like this: <form enctype="multipart/form-data" action...
Palantir
+4  A: 

You should get any checked checkboxes as part of your $_POST array. Unchecked checkboxes will be absent.

Images uploaded (from <input type="file" /> fields) will be in the $_FILES array, if and only if you set your form enctype to "multipart/form-data" (see here).

To get started handling file uploads in PHP, there's an excellent tutorial on W3Schools.

Given the HTML:

<input name="foobar" type="file" id="some_id_for_foobar" />

If you want to get the filename of the uploaded file (i.e. the name of the file as it was on the user's PC), you want:

$name = $_FILES["foobar"]["name"];

If you want the name of the uploaded file on your server, you want:

$location = $_FILES["file"]["tmp_name"];

You may also find the documentation on move_uploaded_file helpful.

Dominic Rodger
Thanks for the answers. I have setup the form to be multipart. What I want from the uploaded file is the name of it - which if i understand correctly is simply a key in the $_FILES array - which I'll google.Re Checkboxes - Thanks.
Thomas Clowes
@Thomas - I've added some details to get you on your way handling the `$_FILES` array - good luck! If this answers your question adequately, could you accept it using the check mark to the left of the vote count on the the answer?
Dominic Rodger