将图像数组上传到服务器文件夹并保存到php中的数据库中

问题描述:

我尝试为该等效文本框值创建动态文本框和动态图像。我得到了我thought.while我需要上传从array.I是动态图像写的代码上传,但只有最后一张图像会uploaded.Help我friends..Here是我的代码将图像数组上传到服务器文件夹并保存到php中的数据库中

PHP代码:

$query = "INSERT INTO tbl_content(user_id,heading,content,content_image) VALUES ";  
for($i=0;$i<$itemCount;$i++) { 

    echo $cimage=$_FILES['image1']['name'][$i];  
    $q2=mysql_query("select max(user_id) as id from tbl_content"); 
    $fe=mysql_fetch_array($q2);  
    $b=$fe['id']+1;  
    if($cimage!="") 
    {  
     $pos=strrpos($cimage,'.'); 
     $mainext=substr($cimage,($pos+1)); 
     $title=$b.'.'.$mainext;  

     move_uploaded_file($_FILES['image1']['tmp_name'][$i],$upload.$title); 

    } else { 
     $title=""; 
    } 

    if(!empty($_POST["name1"][$i]) || !empty($_POST["name2"][$i])) { 
     $itemValues++; 
     if($queryValue!="") { 
      $queryValue .= ","; 
     } 
     $queryValue .= "('".$user_id. "', '".$_POST["name1"][$i]."', '".$_POST["name2"][$i]."', '".$cimage."')";      
    } 
} 
$sql = $query.$queryValue; 
$itemValues;  
if($itemValues!=0) {  
    $result = mysql_query($sql);    
    if(!empty($result)) $message = "Added Successfully."; 
} 

HTML代码:

<DIV class="product-item float-clear" style="clear:both;"> 

<table cellspacing="2"> 
<tr> 
<td><DIV class="float-left"><input type="checkbox" name="item_index[]" /></DIV></td> 
<td><DIV class="float-left">Heading:<input type="text" name="name1[]" style="width:60px" /></DIV></td> 
<td><DIV class="float-left">Content:<textarea type="text" name="name2[]" style="width:90px"/></textarea></DIV></td> 
<td> 
<DIV><input name="image1[]" id="pro_image" type="file" size="45" /></Div></td> 

</DIV> 
</tr> 
</table> 
</body> 

帮助我的朋友.....

+0

的可能的复制[如何选择与多个文件?](http://*.com/questions/1593225/how-to-select-multiple-files-with-input-type-file) – CarlosCarucce

您必须在您输入使用属性:

<input multiple="multiple" name="image1[]" id="pro_image" type="file" size="45" /> 

参见:https://*.com/a/1593259/3435728

+0

我会添加图像动态..所以我写一个代码,如果我点击自动添加更多按钮它会创建另一个输入文件ty pe作为image1的同名。那是我的问题 –

您的代码格式不正确。但是可以理解你的问题。请尝试下面这样。

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
     // Loop $_FILES to exeicute all files 
     foreach ($_FILES['image1']['name'] as $f => $name) {  

      if ($_FILES['image1']['error'][$f] == 0) {    
       if ($_FILES['image1']['size'][$f] > $max_file_size) { 
        $message[] = "$name is too large!."; 
        continue; // Skip large files 
       } 
       elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
        $message[] = "$name is not a valid format"; 
        continue; // Skip invalid file formats 
       } 
       else{ // No error found! Move uploaded files 
        if(move_uploaded_file($_FILES["image1"]["tmp_name"][$f], $path.$name)) 
        //Write insert query here. image name $_FILES["image1"]["name"][$f] 
       } 
      } 
     } 
    }