在PHP&中上传多个文件INSERT到MySQL的路径

时间:2014-12-18 21:33:23

标签: php mysql

我已经设置了一个表单来从PHP脚本上传多个文件,然后将其插入带路径的数据库中。这是我的代码

<form action="" method="post" enctype="multipart/form-data">
<tr class='first'>
    <td>Property Image : </td>
    <td>
        <input type="file" name="pic_upload[]" >
        <input type="file" name="pic_upload[]" >
        <input type="file" name="pic_upload[]" >
    </td>
</tr>
<tr class='first'>
    <td>&nbsp;</td><td><input type="submit" name="create" value="Add"  /></td>
</tr>
</form>
<?php
if(isset($_POST['create'])) {
    $path = "images/";
    for ($i=0; $i<count($_FILES['pic_upload']['name']); $i++) {
        $ext = explode('.', basename( $_FILES['pic_upload']['name'][$i]));
        $path = $path . md5(uniqid()) . "." . $ext[count($ext)-1]; 
        move_uploaded_file($_FILES['pic_upload']['tmp_name'][$i], $path);
    }
    $sql = "INSERT INTO post (`image`) VALUES ('$path');";
    $res = mysqli_query($con,$sql) or die("<p>Query Error".mysqli_error()."</p>");
    echo "<p>Post Created $date</p>";
}
?>  

脚本成功运行,但在列内的数据库端看起来像这样。

图像/ 8de3581eb72ee7b39461df48ff16f4a3.jpg024fae942ae8c550a4bd1a9e028d4033.jpg380cc327df25bc490b83c779511c015b.jpg

请帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

在for循环中移动$path = "images/";。否则,您将在每次迭代后附加到文件名而不重置它。实际上,您根本不需要为该前缀使用变量。您可以立即写下'images/' . md5(uniqid()) . "." . $ext[count($ext)-1]

要将值写入数据库,您可以在每次迭代中运行查询,也可以将路径添加到根据SQL语法转换为逗号分隔的插入列表的数组。

答案 1 :(得分:0)

$files = array_filter($_FILES['lamp']['name']); 

$total = count($files); 

$path = "./asset/save_location/";

// looping for save file to local folder
for( $i=0 ; $i < $total ; $i++ ) {
    $ext = explode('.', basename( $_FILES['lamp']['name'][$i]));
    $tmpFilePath = $_FILES['lamp']['tmp_name'][$i]; 

    if ($tmpFilePath != ""){

        $newFilePath = $path .date('md').$i.".".$ext[count($ext)-1];  //save lampiran ke folder $path dengan format no_surat_tgl_bulan_nourut

        // upload success 
        if(move_uploaded_file($tmpFilePath,$newFilePath)) {
            $success="file uploaded";
        }
    }

} //end for 

//looping for save namefile to database 
for ($i=0; $i<count($_FILES['lamp']['name']); $i++) {
    $ext = explode('.', basename( $_FILES['lamp']['name'][$i]));
    $path = $path .date('md') .$i. "." . $ext[count($ext)-1].";"; 
    $save_dir=substr($path, 22,-1);

}//end for
var_dump($save_dir);die();

答案 2 :(得分:0)

这是对我有用的方法:for循环中包含的所有内容,然后只返回$ fileDest

<?php
if(isset($_POST['submit'])){
  $total = count($_FILES['files']['tmp_name']);
  for($i=0;$i<$total;$i++){
    $fileName = $_FILES['files']['name'][$i];
    $ext = pathinfo($fileName, PATHINFO_EXTENSION);
    $newFileName = md5(uniqid());
    $fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
    if($ext === 'pdf' || 'jpeg' || 'JPG'){
        move_uploaded_file($_FILES['files']['tmp_name'][$i], $fileDest);
    }else{
      echo 'Pdfs and jpegs only please';
    }
  }
}

 ?>



 <!DOCTYPE html>
 <html lang="en" dir="ltr">
   <head>
     <meta charset="utf-8">
     <title></title>
   </head>
   <body>

     <form class="" action="test.php" method="post" enctype="multipart/form-data">
       <input type="file" name="files[]" multiple>
       <button type="submit" name="submit">Upload</button>
     </form>

   </body>
 </html>