将多个 Csv 文件导入 mysql table

Import Multiple Csv file into mysql table


我想将多个 Csv 文件上传到我的数据库中,但是当我尝试添加超过 1 个文件时,它只上传第一个文件并跳过另一个文件,我应该怎么做? 我知道我应该使用 foreach 但我不知道在哪里添加它

这里是上传数据的部分:

 if(isset($_POST["submit"]))
            {   
            
        if($_FILES['file']['name'])
            {
        
                $filename = explode(".", $_FILES['file']['name']);
         if($filename[1] == 'csv')
            {
          
             
                $handle = fopen($_FILES['file']['tmp_name'], "r");
             
             $counter=0;
             while ( ! feof ( $handle ) )
                {     
    if ( $counter === 2)
        break;
 
    $buffer = fgetcsv ( $handle, 5000 ); 
    ++$counter;
                }
             
             while($data = fgetcsv($handle))
                {
        
                    if ($data[42] == "SignInName") {
                 $item0 = mysqli_real_escape_string($connect, $data[42]); 
                                         }
                    $item0 = mysqli_real_escape_string($connect, $data[42]); 
                    $item1 = mysqli_real_escape_string($connect, $data[32]);
                    $item2 = mysqli_real_escape_string($connect, $data[13]);
                    $item3 = mysqli_real_escape_string($connect, $data[19]);
                    $item4 = mysqli_real_escape_string($connect, $data[44]);
                    $item5 = mysqli_real_escape_string($connect, $data[5]);
                     $query = "INSERT into csv(email, password, firstname,lastname,field,country) values('$item0','$item1','$item2','$item3','$item4','$item5')";
                                mysqli_query($connect, $query);
               
             }
                fclose($handle);
                            echo "<script>alert('uploaded');</script>";
             
         }
            else
             
         {
             echo"<script>alert('ERROR ')</script>";    
         }
    }
        }

提前感谢阅读。

Html:

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

php :

if (isset($_POST["submit"])) {
    // Count total files
    $countfiles = count($_FILES['file']['name']);

    // Looping all files
    for ($i = 0; $i < $countfiles; $i++) {
        if ($_FILES['file']['name'][$i]) {
            $filenametmp = $_FILES['file']['name'][$i];
            $filename = explode(".", $filenametmp);

            if ($filename[1] == 'csv') {
                $handle = fopen($_FILES['file']['tmp_name'][$i], "r")
                $counter = 0;
                while (!feof($handle)) {
                    if ($counter === 2)
                        break;
                    $buffer = fgetcsv($handle, 5000);
                    ++$counter;
                }
                while ($data = fgetcsv($handle)) {
                    if ($data[42] == "SignInName") {
                        $item0 = mysqli_real_escape_string($connect, $data[42]);
                    }
                    $item0 = mysqli_real_escape_string($connect, $data[42]);
                    $item1 = mysqli_real_escape_string($connect, $data[32]);
                    $item2 = mysqli_real_escape_string($connect, $data[13]);
                    $item3 = mysqli_real_escape_string($connect, $data[19]);
                    $item4 = mysqli_real_escape_string($connect, $data[44]);
                    $item5 = mysqli_real_escape_string($connect, $data[5]);
                    $query = "INSERT into csv(email, password, firstname,lastname,field,country) values('$item0','$item1','$item2','$item3','$item4','$item5')";
                    mysqli_query($connect, $query);
                }
                fclose($handle);
                echo "<script>alert('uploaded');</script>";
            } else {
                echo "<script>alert('ERROR ')</script>";
            }
        }
    }
}