php - Storing Form element ID in variable?

585

I have an HTML form that contains the following code:

<input id="image_22" name="images[]" type="file"  />
<input id="image_8" name="images[]" type="file"  />   

I'd like to be able to store the form input ID element in a variable (e.g. $imgnumber = 'image_22'). Is this possible? See my comment in the code below:

PHP:

for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;
            $uploaded_files[] = $_FILES['images']['name'][$i];

            if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {

                $number_of_moved_files++;
                // *** COMMENT *** // 
                $imgnumber = CURRENT_FILE_INPUT_ID - this would store e.g. "image_22"
                }

            }

    }

Many thanks for any help with this :-)

212

Answer

Solution:

Only thename and value of inputs are submitted to the server side. You could code the ID into the name instead of using an array of files.

<input id="image_22" name="image_22" type="file"  />
<input id="image_8" name="image_8" type="file"  />

Access on the PHP side:

foreach($_FILES as $imageName => $file)
{
    // $imageName contains image_22 or image_8
    // $file contains the full array for this particular file
}

Edit:

Your updated PHP code could be:

foreach($_FILES as $imageName => $file)
{
    $number_of_file_fields++;
    if ($file['name'] != '') { //check if file field empty or not
        $number_of_uploaded_files++;
        $uploaded_files[] = $file['name'];

        if (move_uploaded_file($file['tmp_name'], $upload_directory . $file['name'])) {

            $number_of_moved_files++;
            // *** COMMENT *** // 
            $imgnumber = $imageName; // image_22 or image_8
        }
    }
}

Failing that, you will have to send the ID as a separate variable, for example using a hidden input or the querystring.

456

Answer

Solution:

You can't do it in pure HTML+PHP for the simple reason that browsers do not send IDs to the server when forms are submitted.

The most obvious solution is to add the relevant information somewhere else, such as thename attribute.

Update:

Rather than:

<input id="image_22" name="images[]" type="file"  />
<input id="image_8" name="images[]" type="file"  />

... do this:

<input name="images[22]" type="file"  />
<input name="images[8]" type="file"  />

Update:

If youprint_r() your input, you'll see what it looks like:

Array
(
    [images] => Array
        (
            [name] => Array
                (
                    [22] => Test.txt
                    [8] => Blah.exe
                )

            [type] => Array
                (
                    [22] => text/plain
                    [8] => application/octet-stream
                )

            [tmp_name] => Array
                (
                    [22] => D:\Server\PHP\tmp\php7D2D.tmp
                    [8] => D:\Server\PHP\tmp\php7D4E.tmp
                )

            [error] => Array
                (
                    [22] => 0
                    [8] => 0
                )

            [size] => Array
                (
                    [22] => 3695
                    [8] => 432
                )

        )

)

People are also looking for solutions to the problem: php - how to inject configs to every controller zend framework 2

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.