duanlu1908 2017-01-01 12:38
浏览 126

如何使用PHP上传和显示多个图像

Insert code :

<form method = "POST" action = "add_visual.php" enctype="multipart/form-data">
 <div class="form-group">
   <label for="exampleInputFile">Image input</label>
   <input type="file" name = "files[]" multiple>
   <button type="submit" name = "upload" class="btn btn- default">Upload</button>
 </div>

Display code :

$count = 0; // to add <div class="row"> after every 4 image display

$sql = "SELECT img_name, address FROM visual ORDER BY visual_id DESC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) 
{

    echo '<div class="col-lg-3 col-md-4 col-xs-6 thumb">';
    echo '<a class="thumbnail" href="#">';
    echo '<img class="img-responsive" src="' .$row["address"] . $row["img_name"] . '" alt="">';
    echo '</a>';
    echo '</div>';

    $count++;
    if($count % 4 == 0)
    {
        echo '</div>';
        echo '<div class="row">';
    }

}
} 

else {
echo "0 results";
}
mysqli_close($conn);

This is how it handles one photo, any way of doing multiple images ? Here is the php process of how the data is input into the form.

<?php

include 'dbconnect.php';

if(isset($_POST["files"]))
{    
/*         
   $file = rand(1000,100000)."-".$_FILES['file']['name'];
   $file_loc = $_FILES['file']['tmp_name'];
   $folder="images/";

   move_uploaded_file($file_loc,$folder.$file);

   $sql="INSERT INTO visual(img_name, address) VALUES('$file', '$folder')";

   mysqli_query($conn,$sql); 
*/
 extract($_POST);
 $error=array();
 $extension=array("jpeg","jpg","png","gif");
 foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
        {
            $file_name=$_FILES["files"]["name"][$key]; 
            $file_tmp=$_FILES["files"]["tmp_name"][$key];
            $ext=pathinfo($file_name,PATHINFO_EXTENSION);
            if(in_array($ext,$extension))
            {
                  if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name))
                {
                    move_uploaded_file($file_tmp=$_FILES["files"] ["tmp_name"][$key],"images/".$file_name);
                }
                /*
                else
                {
                    $filename=basename($file_name,$ext);
                    $newFileName=$filename.time().".".$ext;
                    move_uploaded_file($file_tmp=$_FILES["files"] ["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
                }
                */
            }
            else
            {
                array_push($error,"$file_name, ");
            }
        }
}
?>

Any help on this will be appreciated. I'm kinda lost on how to make multi upload, what am I missing out. Thank you.

  • 写回答

2条回答 默认 最新

  • doufan6544 2017-01-01 14:00
    关注

    <input type="file" name = "file[]" multiple> it should be <input type="file" name = "files[]" multiple>

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?