2017-03-10 69 views
0

在這裏,我的php腳本在服務器上傳圖片。我希望用戶可以在一個記錄上上​​傳多個圖像。現在,在這段代碼中,只有一個圖像可以發佈,用戶的要求是他們可以發佈多個圖像。我不知道如何才能使用數組。請幫忙。感謝提前:)如何使用PHP表單一次性ulpload多個圖像?

<?php 
    include ("connect.php"); 
    if(isset($_POST['submit'])) 
{ 
$event = $_POST['evnt_name']; 
$image_name = $_FILES['evnt_img']['name']; 
$image_type = $_FILES['evnt_img']['type']; 
$image_size = $_FILES['evnt_img']['size']; 
$image_tmp = $_FILES['evnt_img']['tmp_name']; 

if($event=='' && $image_name==''){ 

    echo "<script>alert('Any field is empty')</script>"; 
    exit(); 
} 
if($image_type=="image/jpeg" OR $image_type=="image/png" OR  $image_type=="image/gif") 
{ 
    if($image_size<=50000) 
    { 
     move_uploaded_file($image_tmp,"imagess/$image_name"); 
    } 
    else 
    { 
     echo "<script>alert('image is large, only 50kb size allowed')</script>"; 
     exit(); 
    } 

} 
else{ 
    echo "<script>alert('image type is invalid')</script>"; 
    exit(); 
} 

$query = "insert into event_update (evnt_text,evnt_img) values ('$event','$image_name')"; 

    if(mysqli_query($conn,$query)) 
    { 
    echo "<script>alert('Post has been published')</script>"; 
    exit(); 
    } 
} 
?> 

及以下形式

<div class="col-lg-12"> 
    <form method="POST" action="evntform.php" enctype="multipart/form-data"> 
     <div class="form-group"> 
      <label>Events Name</label> 
      <input type="text" name="evnt_name" placeholder="Write Events Name" class="form-control"> 
     </div> 
     <div class="form-group"> 
      <label>File input</label> 
      <input type="file" name="evnt_img[]"> 
     </div> 
     <button name="submit" type="submit" class="btn btn-default">Submit Button</button> 
    </form> 
</div> 

回答

1

這裏我簡單的引導HTML代碼是一個簡單的例子:

HTML:

<div class="col-lg-12"> 
<form method="POST" action="evntform.php" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <label>Events Name</label> 
     <input type="text" name="evnt_name" placeholder="Write Events Name" class="form-control"> 
    </div> 
    <div class="form-group"> 
     <label>File input</label> 
     <input type="file" name="evnt_img[]" multiple> 
    </div> 
    <button name="submit" type="submit" class="btn btn-default">Submit Button</button> 
</form> 

要選擇的PHP代碼第一圖像上傳:

<?php 
    include ("connect.php"); 
    if(isset($_POST['submit'])) 
{ 
$event = $_POST['evnt_name']; 
$image_name = $_FILES['evnt_img'][0]['name']; 
$image_type = $_FILES['evnt_img'][0]['type']; 
$image_size = $_FILES['evnt_img'][0]['size']; 
$image_tmp = $_FILES['evnt_img'][0]['tmp_name']; 
.... 

您可以使用一個for循環的每個圖像:

for($i=0;$i<count($_FILES['evnt_img']);$i++){ 

$image_name = $_FILES['evnt_img'][$i]['name']; 

} 

Ĵ

+0

好,好..我新受告訴我怎樣才能在使用「迴路」以上代碼.. plz –

+0

上面的for循環計數從0到您陣列中的文件數量:請參閱:https://www.w3schools.com/php/php_looping_for.asp 在您編程時學習! –

+0

好,但現在問題是前端..在形式用戶如何可以選擇多個圖像..只有一個圖像可選。 –

相關問題