Reputation: 660
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.
Upvotes: 1
Views: 192
Reputation: 4055
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
)
)
)
Upvotes: 0
Reputation: 251
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
Upvotes: 0
Reputation:
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.
Upvotes: 4