2017-03-10 22 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

好,但现在问题是前端..在形式用户如何可以选择多个图像..只有一个图像可选。 –

相关问题