I have a script that allows me to upload photos for products in a database. I'd like to upload photos for multiple products via the same form. I construct a dynamic form with one file input per product, with the product id as the identifier. For some reason when I try to receive the files on the other end I'm not getting everything. Here is my code...any ideas?
<form class="form-signin" action="products_csv.php" method="POST" enctype="multipart/form-data" style="max-width:600px !important"> <h2 class="form-signin-heading">Upload Photos</h2> <h4>Product #1</h4> <input type="hidden" name="product_ids[]" value="23" ?> <input type="file" class="form-control" name="photos[23][]" id="photos" multiple><br /><br /> <h4>Product #2</h4> <input type="hidden" name="product_ids[]" value="24" ?> <input type="file" class="form-control" name="photos[24][]" id="photos" multiple><br /><br /> <h4>Product #3</h4> <input type="hidden" name="product_ids[]" value="25" ?> <input type="file" class="form-control" name="photos[25][]" id="photos" multiple><br /><br /> <h4>Product #4</h4> <input type="hidden" name="product_ids[]" value="26" ?> <input type="file" class="form-control" name="photos[26][]" id="photos" multiple><br /><br /> <h4>Product #5</h4> <input type="hidden" name="product_ids[]" value="27" ?> <input type="file" class="form-control" name="photos[27][]" id="photos" multiple><br /><br /> <h4>Product #6</h4> <input type="hidden" name="product_ids[]" value="28" ?> <input type="file" class="form-control" name="photos[28][]" id="photos" multiple><br /><br /> <button class="btn btn-large btn-primary" type="submit">Upload</button> </form> Here is the PHP code:
foreach($_POST as $key => $value) { $$key = $value; } //Upload photos and link to records in DB $path = "uploads/"; // Upload directory $total_count = 0; $total_products = 0; foreach($product_ids as $key => $value) { $query = "SELECT * FROM `variants_queue` WHERE `id` = '$value'"; $rs = mysql_query($query,$con); $row = mysql_fetch_row($rs); $sku = $row[1]; $num_of_photos = count($_FILES["photos"]['name'][$value]); $count = 0; for($i=0;$i<=$num_of_photos-1;$i++) { $filename = $path.$sku."-".$i.".jpg"; $filesize = $_FILES['photos']['size'][$value][$i]; if($filesize > 0) { move_uploaded_file($_FILES['photos']["tmp_name"][$value][$i], $filename); $query2 = "INSERT INTO `variants_photos_queue` (variants_queue_id,filename,size) VALUES ('$value','$filename','$filesize')"; $rs2 = mysql_query($query2,$con); $count++; } } }