在服务器上上传3张图片并在数据库中保存路径

时间:2015-01-13 11:02:48

标签: php mysql sql mysqli phpmyadmin

我有上传单张图片的代码,工作正常。我希望上传3张图片,有代码可以选择多张图片并上传,但我希望通过不同的上传按钮分别上传每张图片并更新数据库中的每张图片

数据库视图

id          image1                       image2                 image3 
1     carouselimage/image1.jpg   carouselimage/image2.jpg   carouselimage/image3.jpg

目前我拥有的前端和后端代码是:

上传图片的表单是

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

saveimage.php

   <?php
ob_start();
require 'connection.php';
if (isset($_POST['submit'])) 
    {
            function GetImageExtension($imagetype)
                {
                    if(empty($imagetype)) return false;
                    switch($imagetype)
                        {
                            case 'image/bmp': return '.bmp';
                            case 'image/gif': return '.gif';
                            case 'image/png': return '.png';
                            default: return false;
                        }
                }




     if (!empty($file["name"])) 
        {   
            foreach($_POST['files'] as $file) 
                {
                    $file_name=$file["name"];   
                    $temp_name=$file["tmp_name"];
                    $imgtype=$fil["type"];     
                    $ext= GetImageExtension($imgtype);    
                    $imagename=image1.$ext;     

                    $target_path = "carouselimage/".$imagename;    

                    if(move_uploaded_file($temp_name, $target_path)) 
                        {
                            $sql = "UPDATE carousel SET image1='$target_path' WHERE id=1"; 
                            if (mysqli_query($con, $sql)) 
                                {
                                    //echo "Record updated successfully";
                                } 
                            else
                                {
                                    //echo "Error updating record: " . mysqli_error($con);
                                } 
                        }
                    else
                        {
                            $error .="Error While uploading image on the server";
                        } 
                }
        }
    }

&GT;

我想要的观点是

enter image description here

并通过单个按钮我希望上传并保存3张图片(也提到了编码)。任何人都可以告诉我,如何修改后端代码上传3张图片。但是我的图像没有上传,他们的路径没有保存在数据库中

4 个答案:

答案 0 :(得分:0)

将图像名称用作name =“files []”

之类的数组

并使用

  <?php
    ob_start();
    require 'connection.php';
            function GetImageExtension($imagetype)
                {
                    if(empty($imagetype)) return false;
                    switch($imagetype)
                        {
                            case 'image/bmp': return '.bmp';
                            case 'image/gif': return '.gif';
                            case 'image/png': return '.png';
                            default: return false;
                        }
                }

    foreach($_POST['files'] as $file) {


     if (!empty($file["name"])) 
                    {   
                        $file_name=$file["name"];   
                        $temp_name=$file["tmp_name"];
                        $imgtype=$fil["type"];     
                        $ext= GetImageExtension($imgtype);    
                        $imagename=image1.$ext;     

                        $target_path = "carouselimage/".$imagename;    

                        if(move_uploaded_file($temp_name, $target_path)) 
                            {
                                $sql = "UPDATE carousel SET image1='$target_path' WHERE id=1"; // here i wish to update image2 and image3 column also as shown in database
                                if (mysqli_query($con, $sql)) 
                                    {
                                        //echo "Record updated successfully";
                                    } 
                                else
                                    {
                                        //echo "Error updating record: " . mysqli_error($con);
                                    } 
                            }
                        else
                            {
                                $error .="Error While uploading image on the server";
                            } 
                    }
    }
        ?>

答案 1 :(得分:0)

Update your html as below,

 <input  name="file[]" type="file">
 <input  name="file[]" type="file">
 <input  name="file[]" type="file">

In php ,

foreach($_POST['file'] as $file) {
//Your Code here
}

答案 2 :(得分:0)

试试这个html代码

输入名称=&#34; uploadedimage1&#34;类型=&#34;文件&#34;

输入名称=&#34; uploadedimage2&#34;类型=&#34;文件&#34;

输入名称=&#34; uploadedimage3&#34;类型=&#34;文件&#34;

试试这个html代码php代码

插入tbl(image1,image2,image3)值(&#39; $ _ FILES [&#39; uploadedimage1&#39;] [&#39; name&#39;]&#39;,&#39; $ _FILES [&#39; uploadedimage2&#39;] [&#39;名称&#39;&#39;&#39; $ _ FILES [&#39; uploadedimage3&#39;] [&#39;名称&# 39;]&#39)

答案 3 :(得分:0)

请参阅此帖子并参考Multiple file upload in php

for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $TARGET_PATH= "carouselimage" . strtolower($_FILES['upload']['name'][$i]);

    //Upload the file into the temp dir
    if( move_uploaded_file($tmpFilePath, $TARGET_PATH) ) {

      //Handle other code here
       $sql = "UPDATE carousel SET image".($i+1)."='$TARGET_PATH' WHERE id=1"; 
       // Run your query 

    }
  }
}