PHP - 通过一种形式的多个输入上传多个文件

时间:2015-05-24 23:04:13

标签: php forms file-upload

我有一个脚本,允许我上传数据库中产品的照片。我想通过同一表单上传多个产品的照片。我构建了一个动态表单,每个产品有一个文件输入,产品ID作为标识符。出于某种原因,当我尝试接收另一端的文件时,我没有得到所有内容。这是我的代码...任何想法?

  <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>

这是PHP代码:

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 个答案:

答案 0 :(得分:3)

这是完整的代码吗? $ product_ids没有任何参考。

请尝试这样。 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++;


    }

}
?>