如何使用数组上传多个图像?

时间:2018-10-10 16:36:50

标签: php html mysql

我有一个表格,如何一张一张地插入数据库。例如:-

 <form method="post">
  <input type="file" name="img[]">
  <input type="file" name="img[]">
  <input type="file" name="img[]">
 </form>

1 个答案:

答案 0 :(得分:0)

if(isset($_POST['submit'])){
if(count($_FILES['img']['name']) > 0){
    //Loop through each file
    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 != ""){

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];

            //save the url and the file
            $filePath = "uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];

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

                $files[] = $shortname;
                //insert into db 
                //use $shortname for the filename
                //use $filePath for the relative url to the file

            }
          }
    }
}

这可能对您有帮助