簡體   English   中英

將圖像數組上傳到服務器文件夾並保存到php數據庫中

[英]upload array of images to server folder and save into database in 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>

幫幫我朋友.....

您的代碼格式不正確。 但是可以理解你的問題是什么。 請像下面這樣嘗試。

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

您必須在輸入中使用多個屬性:

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

另請參閱: https : //stackoverflow.com/a/1593259/3435728

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM