3

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++;
        }

    }

}

1 Answer 1

2

Is this the full code? The $product_ids doesn't have any reference.

Please try like this. HTML:

<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[]" 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[]" 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[]" 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[]" 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[]" 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[]" id="photos" multiple><br /><br />

  <button class="btn btn-large btn-primary" type="submit">Upload</button>

</form>

PHP:

<?php

$path = "uploads/"; // Upload directory
$total_count = 0;
$total_products = 0;
$product_ids = $_POST["product_ids"];
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"]);

    $count = 0;
    $filename = $path.$sku."-".$key.".jpg";
    $filesize = $_FILES['photos']['size'][$key];
    if($filesize > 0) {
            move_uploaded_file($_FILES['photos']["tmp_name"][$key], $filename);
            $query2 = "INSERT INTO `variants_photos_queue` (variants_queue_id,filename,size) VALUES ('$value','$filename','$filesize')";
            $rs2 = mysql_query($query2,$con);
            $count++;


    }

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

3 Comments

how would i know which photo belongs to which product_id?
it's mapped with the hidden input above the file input. i changed the code, i removed for loop inside the foreach.
Okay, but there can be multiple photos per input...Isn't this only grabbing one photo per file input?

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.