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

时间:2017-01-01 12:38:46

标签: php

插入代码:

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

显示代码:

$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);

这是如何处理一张照片,任何方式做多张图片? 这是关于如何将数据输入表单的php过程。

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

对此有任何帮助将不胜感激。我有点迷失在如何进行多重上传,我错过了什么。谢谢。

2 个答案:

答案 0 :(得分:0)

IO.puts inspect(_, pretty: true)它应该是<input type="file" name = "file[]" multiple>

答案 1 :(得分:0)

Prakash是对的,但更重要的是,文件不会存储在$_POST中,这就是if语句所依赖的内容。这种情况永远不会成真。如果要检查$_POST或立即检查文件,请在表单中添加其他字段:

if(isset($_FILES["files"]))
{
    extract($_POST);
    $error=array();
    ...
}