0

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.

1 Answers1

0

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>";
            }
        }
    }
}
Holzer
  • 59
  • 8
  • not working! here is the Error: {count(): Parameter must be an array or an object that implements Countable} –  Oct 27 '20 at 08:56
  • your input must be like this: – Holzer Oct 27 '20 at 09:04
  • NEW Error: Warning: fopen() expects parameter 1 to be a valid path, array given in Warning: feof() expects parameter 1 to be resource, bool given in Warning: fgetcsv() expects parameter 1 to be resource, bool given in –  Oct 27 '20 at 09:07
  • Change the line with fopen() like this : $handle = fopen($_FILES['file']['tmp_name'][$i], "r"); – Holzer Oct 27 '20 at 09:11
  • thank you it work now change the answer so i accept it as an answer! –  Oct 27 '20 at 09:29
  • dude there is an other problem with these files when I upload this (usal.edu.ar-users.csv) i get alert that there is a error this line ( echo "";) but when I rename it to for example test or anything it will work !! what should i change for this? thank in advance!! –  Oct 27 '20 at 09:31
  • I think the problem is the js tag so i suggest you to change it with a plain text – Holzer Oct 27 '20 at 09:36
  • $filenametmp = str_replace(' ','_',$filenametmp); some one told me use this ! can it help where should i add this? sorry about asking too much! –  Oct 27 '20 at 09:53
  • str_replace change a part of string to another string, but for that, i think that it's not will be useful – Holzer Oct 27 '20 at 10:07