1

我正在尝试将多个文件从 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
        ------------------*/
4

1 回答 1

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;
}
于 2013-08-12T12:48:21.193 回答