1

I have a basic script that allows a user to bulk upload products to a database. The first step is uploading a CSV file. Once the file is uploaded, the script displays a page that enables the user to review each product and add one or multiple photos to upload for each of the products.

I am using HTML similar to the following:

<input type="file" class="form-control" name="photos[]" id="photos" multiple>

This HTML input is displayed once per product, all within one HTML form.

When I receive the submission on the server side, it is consolidating all the product photos from all the HTML inputs into one array. The problem is, I do not know which photos belong to which products.

Is there any way to resolve this issue so that I can distinguish between the photos? Each product may have multiple photos, and I'd like to upload each set from its own select box.

3 Answers 3

4

Use a multidimensional array in your <input> names:

<input type="file" class="form-control" name="photos[productid][]" id="photos" multiple>

Where productid is the product ID for each product.

Sign up to request clarification or add additional context in comments.

1 Comment

upvoted this as it was first answered. for additional details see my answer below showing an example of the resulting $_FILES array, note: "photos[id][]" works the same as "photos_*ID*[]" except you can extract the "productid" easier with "photos[id][]" simply by using the index key.
0

Like what they said above, use a multidimensional array.
The array will contain 2 arrays: [the ids][the images].
In HTML it will be something like this:

the loop you have {
<input type="file" name="photos[<?php echo $id ?>][<?php echo $i ?>]" class="form-control">
}

And this is how to receive the array:

<?php 
foreach ($_POST["photos"] as $id) {
    foreach ($id as $photo) {
        $sql[$id][$photo] = "INSERT INTO tableName (Photo, IDProduct) VALUES($photo, $id)";
    }
}
?>

This is only an example, there might be some mistakes in this sample but You have to do this with a multidimensional array and then use the foreach loop to get the data.
Good luck

2 Comments

Hi Elomar, this would work for a single file select but what about a multiple file select, multiple times throughout the form?
So you have products and for each product you can upload more than one image?
0

i am presuming your PHP script is generating the form based on the CSV entries.

by outputting some sort of unique identifier in the photos[] form array, you will be able to connect the photos to the relevant entry.

arbirtrary example:

<?php
$csv_entries = array( 'one', 'two', 'three' );
foreach( $csv_entries as $csv_unique_identifier ) : ?>
<input type="file" class="form-control" name="photos_<?php echo $csv_unique_identifier; ?>[]" id="photos" multiple>
<?php endforeach; ?>

EDIT: changed name="photos[$csv_unique_identifier]" to photos_$csv_unique_identifier[]"

if you print_r( $__FILES__ ) from your submitted form, you will get something like this (make sure to scroll all the way down to see the example completely):

Array
(
    [photos_one] => Array
        (
            [name] => Array
                (
                    [0] => one-2.txt
                    [1] => one-1.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpsXiiL3
                    [1] => /tmp/phpFW4ki3
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 4
                    [1] => 4
                )

        )

    [photos_two] => Array
        (
            [name] => Array
                (
                    [0] => two-2.txt
                    [1] => two-1.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpgouoP2
                    [1] => /tmp/phpRfNsm2
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 4
                    [1] => 4
                )

        )

    [photos_three] => Array
        (
            [name] => Array
                (
                    [0] => 
                )

            [type] => Array
                (
                    [0] => 
                )

            [tmp_name] => Array
                (
                    [0] => 
                )

            [error] => Array
                (
                    [0] => 4
                )

            [size] => Array
                (
                    [0] => 0
                )
        )
)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.