图片上传多个最多四张图片[php]

时间:2016-05-05 08:05:49

标签: php upload image-uploading

这是我上传图片的代码。它工作但只有一个图像。我想更改它,以便我的用户上传最多4张图片。有人可以指导我吗?

if(isset($_FILES['image'])){
    $file_name = $_FILES['image']['name'];
    $file_size =$_FILES['image']['size'];
    $file_tmp =$_FILES['image']['tmp_name'];
    $file_type=$_FILES['image']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));


    $extensions= array("jpeg","jpg","png","gif","");        
    if(in_array($file_ext,$extensions) === false){
        $msg = '<div class="error">Extension not allowed, please choose a JPEG or PNG file.</div>'; 
    }
    if($file_size > 10000000){
        $msg = '<div class="error">File size must not be larger than 10MB</div>';
    }

    if(empty($msg)==true){
        $newname= (rand(1, 99999999999).".".$file_ext);     
        $dir_separator = DIRECTORY_SEPARATOR;
        $folder = "uploads";

        $uploaddate = date("M-d-Y");

        $destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;

        if(!is_dir($destination_path)) mkdir($destination_path);

        $target_path = $destination_path.$newname;
        move_uploaded_file($file_tmp, $target_path);

        if(empty($file_name) == false){
        $file_location = "/".$uploaddate."/".$newname;
         }
        else
         { 
         $file_location = "";
         }

        //SQL Insert code here
        echo "Upload complete";

    }
}

这是我的表格

<form id="support-form" action="" name="support-form" method="POST" enctype="multipart/form-data">
            <div class="subject-message-wrap"> <br/>
                <div class="message-text" style="text-align:center;">
                    <textarea name="message" id="message" rows="5" style="width:98%; max-width: 98%;" placeholder="Write your reply here..." required></textarea>
                </div>
            </div>
            <!---UPLOAD FILE--->
            <div class="wrap-upload">
                <div>
                <label style="color:#009fdb;">&nbsp;&nbsp;&nbsp;Attach an image: </label>
                <input type="file" name="image" id="image" multiple/>

                </div> 
                <br/>
                <div class="submit-button-wrap">
                &nbsp;&nbsp;&nbsp;
                <input type="submit" value="Reply" name="submit" id="submit">&nbsp;&nbsp;&nbsp;<input type="submit" value="Cancel" name="cancel" id="cancel">
                </div>
            </div>
            <!---Code End UPLOAD FILE--->
            </form>

编辑#1: 我的数据库表上的问题。我的表看起来像这样。

table name = support

我在想的是为附加图像位置(网址)添加新的3个字段。还有比我好的其他好主意吗?

3 个答案:

答案 0 :(得分:0)

以下是如何做到这一点:

注意:如果您要上传多张照片,则需要输入多个file

<form action="file-upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="submit" value="Send files" >
</form>

然后您可以像提交按钮一样获取所有图像:

$images = $_POST['images'];
for($i=0;$i<count($images);$i++)
{
// Here you need to repeat your upload whole PHP Code for each photo 
// to upload all of them
}

参考网址

  

http://php.net/manual/en/features.file-upload.multiple.php

你问题的另一部分:

在您的数据库中存储所有4个图像:

解决方案:您无需多个字段即可将这些值保存在其中。您只需将图像名称保存在一个字段中即可:

<?php
$images = array();
$images[0]['images'] = "image1,image2,image3,image4";
$images[1]['images'] = "image1,image2,image3,image4";
$images[2]['images'] = "image1,image2,image3,image4";
//After fetching your data from your database 
//You can simply explode the image column data in order to separate each image value
for($i=0;$i<count($images);$i++) {
$image = explode(",",$images[$i]['images']);
echo "Image 1 :".$image[0]."</br>";
echo "Image 2 :".$image[1]."</br>";
echo "Image 3 :".$image[2]."</br>";
echo "Image 4 :".$image[3]."</br></br></br></br></br>";
}
?>

代码输出:

Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4




Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4




Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4

答案 1 :(得分:0)

更改输入名称:

<input type="file" name="image[]" id="image" multiple />

更改PHP代码以处理多个文件上传。很好的例子:

http://php.net/manual/en/features.file-upload.multiple.php

限制客户端(JavaScript)和服务器(PHP)上的图像数量。

答案 2 :(得分:0)

试试这个

if(isset($_FILES['image'])){


$num_files = count($_FILES['image']['tmp_name']);  // file count


for($i=0; $i < $num_files;$i++)
{

    if($i<4)
    {
      //here checking only four file



$file_name = $_FILES['image']['name'][$i];
$file_size =$_FILES['image']['size'][$i];
$file_tmp =$_FILES['image']['tmp_name'][$i];
$file_type=$_FILES['image']['type'][$i];   
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'][$i])));


$extensions= array("jpeg","jpg","png","gif","");        
if(in_array($file_ext,$extensions) === false){
    $msg = '<div class="error">Extension not allowed, please choose a JPEG or PNG file.</div>'; 
}
if($file_size > 10000000){
    $msg = '<div class="error">File size must not be larger than 10MB</div>';
}

if(empty($msg)==true){
    $newname= (rand(1, 99999999999).".".$file_ext);     
    $dir_separator = DIRECTORY_SEPARATOR;
    $folder = "uploads";

    $uploaddate = date("M-d-Y");

    $destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;

    if(!is_dir($destination_path)) mkdir($destination_path);

    $target_path = $destination_path.$newname;
    move_uploaded_file($file_tmp, $target_path);

    if(empty($file_name) == false){
    $file_location = "/".$uploaddate."/".$newname;
     }
    else
     { 
     $file_location = "";
     }

    //SQL Insert code here
    echo "Upload complete";


    }

}
 }

 }
相关问题