我编写了这段代码,允许读取文件夹,根据文件名插入到不同的表中的 csv 文件的数据。文件处理完毕后,将被移动到另一个文件夹。我的代码运行良好,但第二个 csv 文件长度超过 80 000 行,需要几个小时才能集成到我的数据库中。
如何提高代码的性能?我尝试“加载本地数据”但没有成功......
<?php
include("../connexion.php");
ini_set('max_execution_time', 54000);
$timestamp= date("y-m-d H:i");
$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);
while (($data = fgetcsv($handle, 9000000, ";")) !== false) {
if (empty(array_filter($data))) {
echo "not good";
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
//database entry
$query = "insert into dbo.Y2_Sales (storenumber, storename, date, time, TransRef, stylecode, color, size, quantity, unit_price, SalesExGST, cost, currency)
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]')";
$stmt = $conn->query( $query );
if (!$stmt) { echo $conn->error;}
}
}
交互式爱情