dpg76975
2017-03-10 04:04
浏览 44
已采纳

如何使用PHP表单一次上传多个图像?

Here, my php script for upload image in server. i want user can upload multiple images on one records. right now, in this code at time only one image can post, user's requirement is they can post multiple images at time. I don't idea how can I possible using arrays. please help. Thanks in advance :)

<?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();
   }
 }
?>

And below my simple bootstrap HTML code for form

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

图片转代码服务由CSDN问答提供 功能建议

在这里,我的php脚本用于在服务器中上传图像。 我希望用户可以在一条记录上上传多个图像。 现在,在这个代码中,只有一个图像可以发布,用户的要求是他们可以发布多个图像。 我不知道如何使用数组。 请帮忙。 在此先感谢:)

 &lt;?php 
 include(“connect.php”); 
 if(isset($ _ POST ['submit']))\  n {
 $ 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 ==''&amp;  &amp; $ image_name ==''){
 
 echo“&lt; script&gt; alert('任何字段为空')&lt; / script&gt;”; 
 exit(); 
} 
 nif($ image_type  ==“image / jpeg”OR $ image_type ==“image / png”OR $ image_type ==“image / gif”)
 {
 if if($ image_size&lt; = 50000)
 {
 move_uploaded_file($ image_tmp)  ,“imagess / $ image_name”); 
} 
其他
 {
 echo“&lt; script&gt; alert('图像很大,只允许50kb大小')&lt; / script&gt;”; 
 exit(  ); 
} 
 
} 
else {
 echo“&lt; script&gt; alert('图像类型无效')&lt; / script&gt;”; 
 exit(); 
} 
 
  $ query =“insert into event_update(evnt_text,evnt_img)values('$ event','$ image_name')”; 
 
 if(mysqli)  _query($ conn,$ query))
 {
 echo“&lt; script&gt; alert('帖子已发布')&lt; / script&gt;”; 
 exit(); 
} 
} 
  ?&gt; 
   
 
 

在我的简单引导程序HTML代码下面

 &lt; div class =“col  -lg-12“&gt; 
&lt; form method =”POST“action =”evntform.php“enctype =”multipart / form-data“&gt; 
&lt; div class =”form-group“&gt; \  n&lt; label&gt;事件名称&lt; / label&gt; 
&lt; input type =“text”name =“evnt_name”placeholder =“写事件名称”class =“form-control”&gt; 
&lt; / div&gt; \  n&lt; div class =“form-group”&gt; 
&lt; label&gt;文件输入&lt; / label&gt; 
&lt; input type =“file”name =“evnt_img []”&gt; 
&lt; / div&gt  ; 
&lt; button name =“submit”type =“submit”class =“btn btn-default”&gt;提交按钮&lt; / button&gt; 
&lt; / form&gt; 
&lt; / div&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doson1998 2017-03-10 04:53
    已采纳

    Here is a quick example:

    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 code to select the first image uploaded:

      <?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'];
    ....
    

    You can use a for loop for every image:

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

    J

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题