如何将多个图像从HTML上传到FIles?

时间:2013-08-12 12:00:16

标签: php html mysql database image-uploading

我正在尝试将多个文件从HTML表单上传到文件夹并将信息存储在我的数据库中。我编写了以下代码,但它无法正常工作。如果我上传1张图片并离开另外两张图片字段,那么它会存储3张相同名称和相同图片的图片,如果我在三个不同的字段中上传3张不同的图片,那么它只需要最后一张图片并用其替换其他图片,并显示为所有人提供相同的图像。

请指导我。

HTML Image Fields

<file> <Image2> <Image3>

PHP代码

        /*-------------------
        IMAGE QUERY 
        ---------------*/
        $allowedExts = array("gif", "jpeg", "jpg", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));


    if ((($_FILES["file"]["type"]   == "image/gif")
    ||  ($_FILES["image2"]["type"]  == "image/gif")
    ||  ($_FILES["image3"]["type"]  == "image/gif")
    ||  ($_FILES["file"]["type"]    == "image/jpeg")
    ||  ($_FILES["image2"]["type"]  == "image/jpeg")
    ||  ($_FILES["image3"]["type"]  == "image/jpeg")
    ||  ($_FILES["file"]["type"]    == "image/jpg")
    ||  ($_FILES["image2"]["type"]  == "image/jpg")
    ||  ($_FILES["image3"]["type"]  == "image/jpg")
    ||  ($_FILES["file"]["type"]    == "image/pjpeg")
    ||  ($_FILES["image2"]["type"]  == "image/pjpeg")
    ||  ($_FILES["image3"]["type"]  == "image/pjpeg")
    ||  ($_FILES["file"]["type"]    == "image/x-png")
    ||  ($_FILES["image2"]["type"]  == "image/x-png")
    ||  ($_FILES["image3"]["type"]  == "image/x-png")
    ||  ($_FILES["file"]["type"]    == "image/png")
    ||  ($_FILES["image2"]["type"]  == "image/png")
    ||  ($_FILES["image3"]["type"]  == "image/png"))
    //&& ($_FILES["file"]["size"] < 200000)
    && in_array($extension, $allowedExts))

      {
      if ($_FILES["file"]["error"] > 0 && $_FILES["image"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>";

        echo "Upload: " . $_FILES["image2"]["name"] . "<br>";
        echo "Type: " . $_FILES["image2"]["type"] . "<br>";
        echo "Size: " . ($_FILES["image2"]["size"] / 200000) . " kB<br>";




            $image_name=        $_FILES["file"]["name"];  
            $random_name=       rand().$_FILES["file"]["name"];


            $image_name2=       $_FILES["image2"]["name"];  
            $random_name2=      rand().$_FILES["image2"]["name"];


            $image_name3=       $_FILES["image3"]["name"];  
            $random_name3=      rand().$_FILES["image3"]["name"];






            $path=              move_uploaded_file($_FILES["file"]["tmp_name"],
                                        "upload/products/" . $random_name);


            $path2=             move_uploaded_file($_FILES["image2"]["tmp_name"],
                                        "upload/products/" . $random_name);     


            $path3=             move_uploaded_file($_FILES["image3"]["tmp_name"],
                                        "upload/products/" . $random_name); 



             $folder="upload/products/" .$random_name;  

             $folder2="upload/products/" .$random_name; 

             $folder3="upload/products/" .$random_name; 




                //echo "Stored in: " . "upload/" .rand(). $_FILES["file"]["name"];  
                echo "Stored in:    "."upload/products/". $random_name;

          /*
                $sql = "INSERT INTO `category_images` (`image_name`,`image_location`) VALUES 
                    ('".$image_name."', '".$path."')";
            */      



                $sql = "Insert into product_images (product_id,name,images) 
                    VALUES ($current_id,'$image_name', '$folder')";


                $sql2 = "Insert into product_images (product_id,name,images) 
                    VALUES ($current_id,'$image_name2', '$folder2')";


                $sql3 = "Insert into product_images (product_id,name,images) 
                    VALUES ($current_id,'$image_name3', '$folder3')";

                $result = mysql_query($sql);
                mysql_query($sql2);
                mysql_query($sql3);

                if ($result)
                 {

                    echo "successfull"; 
                 } 
                 else {
                        echo mysql_error();
        }


          }
        }

    else
      {
      echo "Invalid file";
      }


        /*-----------------
        IMAGE QUERY END
        ------------------*/

1 个答案:

答案 0 :(得分:0)

对于上传多个文件,最好将输入字段名称用作数组,这样上传的文件就可以很好地组织成一个易于处理的数组。所以你的上传表格看起来应该是这样的

<form action="upload.php" method="post" enctype="multipart/form-data"> Image1: <input name="image[]" type="file" /><br /> Image2: <input name="image[]" type="file" /><br /> Image3: <input name="image[]" type="file" /><br /> Image4: <input name="image[]" type="file" /><br /> Image5: <input name="image[]" type="file" /><br /> <input type="submit" value="Upload" /> </form>

并在upload.php中

if($_FILES['image'])
{
//organise the array :: refer http://php.net/manual/en/features.file-upload.multiple.php for more details
$images = reArrayFiles($_FILES['image']);
$image_count = count($_FILES['image']['name']);
for ($i = 0; $i < $image_count; $i++)
{
    if($images[$i]['name']!='' && $images[$i]['tmp_name']!='')
    {

        $path = 'upload/products/';
        $saved = 0;
        # Upload file to folder
        $filename = basename($images[$i]['tmp_name']);
        $filename_array = explode('.', $filename);
        $ext = strtolower(end($filename_array));
                    $image_name = date('YmdHis').'.'.$ext; //random file naming
        if(in_array(strtoupper($ext), $allowed_ext)) { 
            $folder = $path . $image_name;
            $saved = move_uploaded_file($images[$i]['tmp_name'], $folder);
        } 
        if($saved)
        {
            $sql = "INSERT INTO product_images (product_id,name,images) VALUES ($current_id,'$image_name', '$folder')";
            if(mysql_query($sql))
                echo "successfull"; 
        }
        else
        {
            echo 'Failed to upload file';
        }
    }
}
}

function rearrange( $arr ){
foreach( $arr as $key => $all ){
    foreach( $all as $i => $val ){
        $new[$i][$key] = $val;   
    }   
}
return $new;
}