0

enter image description here如何上傳多個產品的多個圖像一次

我一個基於電子商務網站,該網站是賣產品,如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"), ''); 


    } 

} 

?> 

回答

0

我已經解決了這個自己

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