如何一次上传多个产品的多张图片

How to Upload Multiple Images for multiple products at once

我在一个基于电子商务的网站上工作,该网站销售 T 恤印刷、咖啡杯印刷等产品。 我想为多个产品上传多个图像文件,但它不起作用我附上了我的 html 表单的屏幕截图以及该文件的 php 代码。

请帮我解决这个问题。 提前致谢。

<?php
if($_POST)
{
    $userid=$_SESSION['new_userid'];
    $date = date("Y-m-d H:i:s");
    foreach($_POST['productid'] as $row=>$productid)
    {
    $pid  = mysql_real_escape_string($productid);       
    $quantity = mysql_real_escape_string($_POST['quantity'][$row]);
    foreach($_FILES['file']['tmp_name'] as $key => $tmp_name )
{
        $file_name = time().$_FILES['file']['name'][$key][$row];
        $file_size =$_FILES['file']['size'][$key][$row];
        $file_tmp =$_FILES['file']['tmp_name'][$key][$row];
        $file_type=$_FILES['file']['type'][$key][$row]; 
        if($file_size > 199097152){
            $errors[]='File size must be less than 19 MB';
        }
        $desired_dir="uploads/orders/";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
        }else{
                print_r($errors);
        }
            $tempArr[] = $file_name;

}
$files_360 .= implode('*',$tempArr);

//Project images            

    $insert = $DB_class->insert('orders',array("userid"=>$userid,"proid"=>$pid,"quantity"=>$quantity,"images"=>$files_360,"date"=>$date,"status"=>"pending"), '');


    }

}

?>

我自己解决了这个问题

<?php
    if($_POST)
    {
        $a = 1;
        $userid=$_SESSION['new_userid'];
        $date = date("Y-m-d H:i:s");
        foreach($_POST['productid'] as $row=>$productid)
        {
                $tempArr = "";
                $pid  = mysql_real_escape_string($productid);       
                $quantity = mysql_real_escape_string($_POST['quantity'][$row]);
                foreach($_FILES['multifile'.$a]['tmp_name']['file'] as $key => $tmp_name )
                {
                    $file_name = time().$_FILES['multifile'.$a]['name']['file'][$key];
                    $file_size =$_FILES['multifile'.$a]['size']['file'][$key];
                    $file_tmp =$_FILES['multifile'.$a]['tmp_name']['file'][$key];
                    $file_type=$_FILES['multifile'.$a]['type']['file'][$key];   
                    if($file_size > 199097152){
                        $errors[]='File size must be less than 19 MB';
                    }
                    $desired_dir="uploads/orders/";
                    if(empty($errors)==true){
                        if(is_dir($desired_dir)==false){
                            mkdir("$desired_dir", 0700);        // Create directory if it does not exist
                        }
                        if(is_dir("$desired_dir/".$file_name)==false){
                            move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                        }else{                                  // rename the file if another one exist
                            $new_dir="$desired_dir/".$file_name.time();
                             rename($file_tmp,$new_dir) ;               
                        }
                    }else{
                            print_r($errors);
                    }
                        $tempArr[] = $file_name;
                        $files_360 = "";


                }
                $files_360 = implode('*',$tempArr);

    //Project images            

        $insert = $DB_class->insert('orders',array("userid"=>$userid,"proid"=>$pid,"quantity"=>$quantity,"images"=>$files_360,"date"=>$date,"status"=>"pending"), '');

        $a++;
        }

    }

    ?>

这是我的 html 代码

<input type="file" name="multifile<?php echo $pro_id; ?>[file][]" multiple>