duanquezhan7268 2017-10-16 05:32
浏览 38

如何在php中使用implode函数动态上传图像?

error snapshot I am getting this error multiple times.This code what ever you send I tested it.
In DB, I taken the property_id is Integer type and Property_img is varchar

<?php
 if(isset($_POST['submit'],$_POST['property_id'],$_FILES['files'])){  // check that needed superglobals are set
    $query="INSERT INTO img_tbl(`property_id`,`property_img`) VALUES(?,?)"; //'$property_id','$join_string')";
    if($stmt=$conn->prepare($query)){
        foreach($_FILES['files']['tmp_name'] as $key=>$tmp_name){
            $property_img[]=$_FILES['files']['name'][$key];
            $join_string=implode(' ',array_slice($_FILES['files']['name'][$key],0,5));
            copy($tmp_name,'uploads/'.$join_string);
        }
// I don't know the value type of $property_id.  If integer, use i instead of s.
        if(!$stmt->bind_param('is',$property_id,$join_string)){
            echo "bind failed";  // $stmt->error;  // do not echo when public
        }elseif(!$stmt->execute()){
            echo "execute failed";  // $stmt->error;  // do not echo when public
        }else{
            header('location:listpro_img.php');
        }
        $stmt->close(); 
    }else{
        echo "prepare failed"; // $mysqli->error;  // do not echo when public
    }
    $conn->close();
}else{
    echo "insufficient data submitted";
}
?>

-----------Here is HTML code for uploading n number of images at a time------

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <link rel="shortcut icon" href="img/favicon.png">  
  </head>
<script>
</script>
  <body id="top">



    <!-- begin:content -->
    <div id="content">
      <div class="container">
        <div class="row">

                    <!-- Add form Code from Here -->

                          <form method="post" enctype="multipart/form-data">
                          <div class="col-sm-12">

                             <div class="col-sm-6"><label>Property Id:</label></div>

                             <div class="col-sm-6 btn btn-info">    
                                   <input type="text" name="property_id"/></div>    
                              <div class="col-sm-6"><label>Property Image:</label></div>                              
                                  <input id="fileupload" type="file" name="files[]" multiple /><br>      

                               <div class="col-sm-12">
                              <input type="submit" name="submit">
                             </div>
                          </div>                                   
                       </form>

                    <!--Form code Ends Here -->
                  </div>
                </div>           
            </div>


  </body>
</html>
  • 写回答

1条回答 默认 最新

  • doutun1362 2017-10-16 07:37
    关注

    Okay, I ran your html on one of my servers to see what kind of $_POST & $_FILES data I was dealing with. I uploaded three small jpgs and got this:

    $_POST=['property_id'=>'test','submit'=>'Submit'];
    $_FILES=['files'=>[
                'name'=>['4x4.jpg','catfish.jpg','calc.jpg'],
                'type'=>['image/jpeg','image/jpeg','image/jpeg'],
                'tmp_name'=>['/tmp/phps6I8zU','/tmp/phpgbmn2z','/tmp/phpo9wSgg'],
                'error'=>[0,0,0],
                'size'=>[14243,43314,36173]
            ]
        ];
    

    *Mind you, I needed to remove the btn btn-info classes so that I could actually access the text field.


    From this I realized that you were calling implode() from inside your foreach() loop and that was fouling things up. $join_string was getting called on each iteration (1 for each file uploaded). Even if implode() was receiving an array (and it wasn't), you were overwriting $join_string each time.

    This is my updated code -- loaded with error checking to help you to isolate and mop up any left over issues:

    if(isset($_POST['submit'],$_POST['property_id'],$_FILES['files'])){  // check that needed superglobals are set
        $query="INSERT INTO img_tbl(`property_id`,`property_img`) VALUES (?,?)";
        if($stmt=$conn->prepare($query)){
            foreach($_FILES['files']['tmp_name'] as $key=>$tmp_name){
                if(!copy($tmp_name,"uploads/{$_FILES['files']['name'][$key]}")){  // // not move_uploaded_file() ?
                    echo "<div>copy error @ $tmp_name & uploads/{$_FILES['files']['name'][$key]}</div>";
                }
            }
            if(!$stmt->bind_param('ss',$_POST['property_id'],implode(' ',$_FILES['files']['name']))){
                echo "bind failed";  // $stmt->error;  // do not echo when public
            }elseif(!$stmt->execute()){
                echo "execute failed";  // $stmt->error;  // do not echo when public
            }else{
                header('location:listpro_img.php');
            }
            $stmt->close(); 
        }else{
            echo "prepare failed"; // $conn->error;  // do not echo when public
        }
        $conn->close();
    }else{
        echo "insufficient data submitted";
    }
    

    I've written all of the superglobal checks into a single isset() call. This syntax is valid, see the manual if you are curious.

    Your original process was using unsanitized data in your query which is not safe. I have implemented a mysqli prepared statement with placeholders for you. I don't know the expected data type of $property_id so I've assigned it as a string (s) but it might possibly be an integer (i) -- you will have to determine the right declaration there.

    Make sure there is enough length in your property_img table column to receive the full length of your space-delimited image file names. If there are many files uploaded and/or their names are relatively long, they might get truncated in a VARCHAR(255) field.

    Edit: if $propert_id is to be an integer, you should limit the input field to numeric values only (a few ways to do this). Then in the form receiving code validate the submitted value to be safe. As I mentioned earlier, replace s with i in the bind call.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料