phpsqlcsvmultiple-input

Import Multiple Csv file into mysql table



I want to upload multiple Csv file into my database but when I try to add more that 1 file it only upload first file and skip the other what should I do for this? I know that I should use foreach but I don't know where to add it

here is the part that upload the data:

 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>";    
         }
    }
        }

thanks in advance for reading.


Solution

  • 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>";
                }
            }
        }
    }