如何在 php 数据库中插入 csv 文件的第一行?

How to insert the first line of a csv file in a php database?

我有两个 csv 文件,第一个 csv 文件 BI1_2020.csv 像这样:

391;FR-RC O/O HOSSEGOR MEN;07/21/2020;0021000002509;GR01-S8;8666-CLEAR;TU;1;15;12.5;0;EUR
391;EN-RC O/O HOSSEGOR MEN;07/21/2020;0021000002510;WLY9BO-RC;1000-WHITE;2;1;22.99;19.16;4.81;EUR
391;FR-RC O/O HOSSEGOR MEN;07/21/2020;0021000002510;WLY5DB-RC;40-RED;8;1;19.99;16.66;4.2;EUR
391;EN-RC O/O HOSSEGOR MEN;07/21/2020;0021000002510;OBOAU4-RC;90-BLACK;2;1;39.99;33.33;11.27;EUR
391;FR-RC O/O HOSSEGOR MEN;07/21/2020;0021000002515;4OCEAN-HF;40-RED;TU;1;20;16.67;0;EUR

还有另一个 BI2_2020.csv 是这样的:

27336;00203-ET1;90-BLACK;9.5;2
27336;00203-ET1;90-BLACK;10;1
27336;00203-ET1;90-BLACK;12;1
27396;00203-ET1;90-BLACK;9.5;3
...

我写了一段代码,根据文件名,BI1 或 BI2 将填充我数据库中表格的 csv 文件中的数据。该代码运行良好,但对于这两个文件,它都没有插入 csv 文件的第一行。我该如何解决这个问题?

<?php
// Starting clock time in seconds 
$time_start = microtime(true); 

include("../connexion.php");

ini_set('max_execution_time', 54000);
$timestamp = date("Y-m-d H:i:s");

$dir   = 'D:/xampp/htdocs/retail_BI/test/';
$allFiles = scandir($dir);
$dest = 'D:/xampp/htdocs/retail_BI/test/files/';

foreach($allFiles as $file) {

    if (!in_array($file,array(".","..")))
    { 
        $file = $dir.$file;
        $filename = basename( $file );
        
        if ( strpos( $filename, 'BI1_' ) === 0 ) {
            if (($handle = fopen($filename, "r")) !== false) {
            
                //To remove BOM in the fist cell
                fseek($handle, 3);
            
                $bi1_values =  array();
                while (($data = fgetcsv($handle, 9000000, ";")) !== false) {
                    if (empty(array_filter($data))) {
                        continue;
                    }
 
                    $date = DateTime::createFromFormat('d/m/Y H:i:s A', $data[2]);
                    // if ($date === false) {
                        // break;
                    // }

                    $date1 = $date->format('Y-m-d'); // 2020-07-07
                    $date2 = $date->format('Hi A'); // 1247 AM
            
                    $bi1_values[] = "('$data[0]', '$data[1]','$date1','$date2','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]')";                 
                    if (count($bi1_values) == 1000) { 
                        $query = "INSERT INTO dbo.Y2_Sales (storenumber, storename, date, time, TransRef, stylecode, color, size, quantity, unit_price, SalesExGST, cost, currency) VALUES " . implode(',', $bi1_values);
                        $stmt = $conn->query( $query );
                        if (!$stmt) {                           
                            $file1 = fopen("errors/erreur_BI1.txt", "w");
                            fwrite($file1,$query);
                            fclose($file1);
                            // $file1 = "errors/erreur_BI1.txt";                      
                            // file_put_contents($file1, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                            // echo $conn->error; }
                        }
                        $bi1_values = array();
                    }   
                }
                if (!empty($bi1_values)) { 
                    $query = "INSERT INTO dbo.Y2_Sales (storenumber, storename, date, time, TransRef, stylecode, color, size, quantity, unit_price, SalesExGST, cost, currency) VALUES " . implode(',', $bi1_values);
                    $stmt = $conn->query( $query );
                    if (!$stmt) { 
                            $file1 = fopen("errors/erreur_BI1.txt", "w");
                            fwrite($file1,$query);
                            fclose($file1);
                        // $file1 = "errors/erreur_BI1.txt";                      
                        // file_put_contents($file1, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                        // echo $conn->error; }
                    }
                }   
                        
                fclose($handle);
                
                //Moving the file to another folder             
                if(!rename($file, $dest . $filename)) { 
                    echo "error copy";
                }
            }
                    
        } elseif ( strpos( $filename, 'BI2_' ) === 0 ) {
    
            if (($handle = fopen($filename, "r")) !== false) {
                  
                // To remove BOM in the fist cell
                fseek($handle, 3);
            
                $bi2_values =  array();
                while (($data = fgetcsv($handle, 9000000, ";")) !== false) {        
                    // print_r($data);
                    if (empty(array_filter($data))) {
                        continue;
                    }
                    $bi2_values[] = "('$data[0]', '$data[1]','$data[2]','$data[3]','$data[4]', '$timestamp')";                 
                    if (count($bi2_values) == 1000) { 
                        $query = "INSERT INTO dbo.Y2_Inventory (storenumber, stylecode, colour, size, units, timestamp) VALUES " . implode(',', $bi2_values);
                        $stmt = $conn->query( $query );
                        
                        if (!$stmt) { 
                                $file2 = fopen("errors/erreur_BI2.txt", "w");
                                fwrite($file2,$query);
                                fclose($file2);
                            // $file2 = "errors/erreur_BI2.txt";                      
                            // file_put_contents($file2, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                            // echo $conn->error;}
                        }   
                        $bi2_values = array();
                    } 
                }
                if (!empty($bi2_values)) {
                    $query = "INSERT INTO dbo.Y2_Inventory (storenumber, stylecode, colour, size, units, timestamp) VALUES " . implode(',', $bi2_values);
                    $stmt = $conn->query( $query );
                        
                    if (!$stmt) { 
                                $file2 = fopen("errors/erreur_BI2.txt", "w");
                                fwrite($file2,$query);
                                fclose($file2);
                        // $file2 = "errors/erreur_BI2.txt";                      
                        // file_put_contents($file2, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                        // echo $conn->error;}

                    }
                }
                fclose($handle);

                // Moving the file to another folder             
                if(!rename($file, $dest . $filename)) { 
                    echo "error copy";
                } 
                    
            }

        }   elseif ( strpos( $filename, 'BI3_' ) === 0 ) {
    
            if (($handle = fopen($filename, "r")) !== false) {
                  
                // To remove BOM in the fist cell
                fseek($handle, 3);
            
                $bi3_values =  array();
                while (($data = fgetcsv($handle, 9000000, ";")) !== false) {        
                    // print_r($data);
                    if (empty(array_filter($data))) {
                        continue;
                    }
                    $bi3_values[] = "('$data[0]', '$data[1]','$data[2]','$data[3]','$data[4]', '$data[5]')";                 
                    if (count($bi3_values) == 1000) { 
                        $query = "INSERT INTO dbo.Y2_Style (stylecode, brandname, department, category, description, season) VALUES " . implode(',', $bi3_values);
                        $stmt = $conn->query( $query );
                        
                        if (!$stmt) { 
                                $file3 = fopen("errors/erreur_BI3.txt", "w");
                                fwrite($file3,$query);
                                fclose($file3);
                            // $file3 = "errors/erreur_BI3.txt";                      
                            // file_put_contents($file3, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                            // echo $conn->error;}
                        }   
                        $bi3_values = array();
                    } 
                }
                if (!empty($bi3_values)) {
                    $query = "INSERT INTO dbo.Y2_Style (stylecode, brandname, department, category, description, season) VALUES " . implode(',', $bi3_values);
                    $stmt = $conn->query( $query );
                        
                    if (!$stmt) { 
                                $file3 = fopen("errors/erreur_BI3.txt", "w");
                                fwrite($file3,$query);
                                fclose($file3);
                                // $file3 = "errors/erreur_BI3.txt";                      
                                // file_put_contents($file3, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                                // echo $conn->error;}

                    }
                }
                fclose($handle);

                // Moving the file to another folder             
                if(!rename($file, $dest . $filename)) { 
                    echo "error copy";
                } 
                    
            }

        }
    }
}
$texte = "Process took ". number_format(microtime(true) - $time_start, 2). " seconds.";

file_put_contents("time.txt", $texte);
  
echo "good !";


?>

在向其中添加任何内容之前,您正在检查 count($bi1_values)。所以计数将永远为 0,你不会添加到它。您应该无条件地将值行添加到数组,然后使用计数来确定是否执行查询以插入所有这些行。

循环内还有 fclose($handle);,这将阻止读取文件的其余部分。应该是在循环完成后。

<?php
// Starting clock time in seconds 
$time_start = microtime(true); 

include("../connexion.php");

ini_set('max_execution_time', 54000);
// $timestamp= date("y-m-d H:i");
$timestamp = date("Y-m-d H:i:s");

$dir   = 'D:/xampp/htdocs/retail_BI/test/';
$allFiles = scandir($dir);
$dest = 'D:/xampp/htdocs/retail_BI/test/files/';

foreach($allFiles as $file) {

    if (!in_array($file,array(".","..")))
    { 
        $file = $dir.$file;
        $filename = basename( $file );
        
        if ( strpos( $filename, 'BI1_' ) === 0 ) {
            if (($handle = fopen("$filename", "r")) !== false) {
            
                //To remove BOM in the fist cell
                fseek($handle, 3);
            
                $bi1_values =  array();
                while (($data = fgetcsv($handle, 9000000, ";")) !== false) {
                    if (empty(array_filter($data))) {
                        continue;
                    }
 
                    $date = DateTime::createFromFormat('d/m/Y H:i:s A', $data[2]);
                    if ($date === false) {
                        break;
                    }

                    $date1 = $date->format('Y-m-d'); // 2020-07-07
                    $date2 = $date->format('Hi A'); // 1247 AM
            
                    $bi1_values[] = "('$data[0]', '$data[1]','$date1','$date2','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]')";                 
                    if (count($bi1_values) == 1000) { 
                        $query = "INSERT INTO dbo.Y2_Sales (storenumber, storename, date, time, TransRef, stylecode, color, size, quantity, unit_price, SalesExGST, cost, currency) VALUES " . implode(',', $bi1_values);
                        $stmt = $conn->query( $query );
                        if (!$stmt) { 
                            $file1 = "erreur_BI1.txt";                      
                            file_put_contents($file1, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                            // echo $conn->error; }
                        }
                        $bi1_values = array();
                    }   
                }
                if (!empty($bi1_values)) { 
                    $query = "INSERT INTO dbo.Y2_Sales (storenumber, storename, date, time, TransRef, stylecode, color, size, quantity, unit_price, SalesExGST, cost, currency) VALUES " . implode(',', $bi1_values);
                    $stmt = $conn->query( $query );
                    if (!$stmt) { 
                        $file1 = "erreur_BI1.txt";                      
                        file_put_contents($file1, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                        // echo $conn->error; }
                    }
                }   
                        
                fclose($handle);
                
                //Moving the file to another folder             
                if(!rename($file, $dest . $filename)) { 
                    echo "error copy";
                }
            }
                    
        } else if ( strpos( $filename, 'BI2_' ) === 0 ) {
    
            if (($handle = fopen("$filename", "r")) !== false) {
                  
                // To remove BOM in the fist cell
                fseek($handle, 3);
            
                $bi2_values =  array();
                while (($data = fgetcsv($handle, 9000000, ";")) !== false) {        
                    // print_r($data);
                    if (empty(array_filter($data))) {
                        continue;
                    }
                    $bi2_values[] = "('$data[0]', '$data[1]','$data[2]','$data[3]','$data[4]', '$timestamp')";                 
                    if (count($bi2_values) == 1000) { 
                        $query = "INSERT INTO dbo.Y2_Inventory (storenumber, stylecode, colour, size, units, timestamp) VALUES " . implode(',', $bi2_values);
                        $stmt = $conn->query( $query );
                        
                        if (!$stmt) { 
                            $file2 = "erreur_BI2.txt";                      
                            file_put_contents($file2, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                            // echo $conn->error;}
                        }   
                        $bi2_values = array();
                    } 
                }
                if (!empty($bi2_values)) {
                    $query = "INSERT INTO dbo.Y2_Inventory (storenumber, stylecode, colour, size, units, timestamp) VALUES " . implode(',', $bi2_values);
                    $stmt = $conn->query( $query );
                        
                    if (!$stmt) { 
                        $file2 = "erreur_BI2.txt";                      
                        file_put_contents($file2, $query . PHP_EOL, FILE_APPEND | LOCK_EX);
                        // echo $conn->error;}

                    }
                }
                fclose($handle);

                // Moving the file to another folder             
                if(!rename($file, $dest . $filename)) { 
                    echo "error copy";
                } 
                    
            }
        }
    }
}
$texte = "Process took ". number_format(microtime(true) - $time_start, 2). " seconds.";

file_put_contents("time.txt", $texte, FILE_APPEND | LOCK_EX);
  
echo "good !";


?>