Storing form field id in a variable during file upload

Hello!

I have a basic script that allows me to upload multiple files to my server - yay!

Now, I’d like to be able to associate a variable with the input element that holds the image.

Dream world would be storing $imgnumber = “9”; in my loop if I upload an image to the image_9 field.

Here is a sample of the HTML code:

<input id="image_9" name="images[]" type="file"  />	
	<input id="image_11" name="images[]" type="file"  />	

Here is a sample of the PHP code:

if (isset($_POST['Submit'])) {
    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
    $upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
    /**
     * we get a $_FILES['images'] array ,
     * we process this array while iterating with simple for loop
     * you can check this array by print_r($_FILES['images']);
     */
    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++;
                // $varnumber = FORM_ELEMENT_ID
            }

        }

    }
    echo "Number of File fields created $number_of_file_fields.<br/> ";
    echo "Number of files submitted $number_of_uploaded_files . <br/>";
    echo "Number of successfully moved files $number_of_moved_files . <br/>";
    echo "File Names are <br/>" . implode(',', $uploaded_files);
}

Many thanks for any pointers here :smiley:

You can specify the key in your form fields and access that key’s index# directly in PHP.


<input id="image_9" name="images[9]" type="file"  />
<input id="image_11" name="images[11]" type="file"  />

Hi kduv,

Thanks for the reply.

Can you explain how I retrieve the key’s index in my for loop example above?

Use a foreach loop instead of a for loop. It was designed specifically for iterating through arrays and objects.

In this example, you can access the key VIA the $key variable. I add each non-empty key to an array just for reference, but you can do whatever you want with it. Using something like this you can even name your form fields with named keys instead of just numbers… IE: name="images[thisimage]"


if (isset($_POST['Submit'])) {

    $non_empty_keys = array();
    $number_of_file_fields = c;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
    $upload_directory = dirname(__file__) . '/uploaded/';

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

            $non_empty_keys[] = $key;
            $number_of_uploaded_files++;
            $uploaded_files[] = $_FILES['images']['name'][$key];

            if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $upload_directory . $_FILES['images']['name'][$key])) {
                $number_of_moved_files++;
            }
        }
    }

    echo "These are the keys for non-empty fields: ", implode(', ', $non_empty_keys), '<br>';
    echo "Number of File fields created $number_of_file_fields.<br/> ";
    echo "Number of files submitted $number_of_uploaded_files . <br/>";
    echo "Number of successfully moved files $number_of_moved_files . <br/>";
    echo "File Names are <br/>" . implode(',', $uploaded_files);
}