Bulk data import is a common requirement in many web applications. ThinkPHP6 offers robust file upload support, and MySQL includes powerful native import capabilities, especially the LOAD DATA INFILE command. This article walks through how to integrate both for a reliable and efficient data import process.
Before starting, ensure the following prerequisites are met:
We begin by creating a file upload interface to allow users to select the data file they want to import.
use think\facade\View;
use think\facade\Request;
public function uploadFile()
{
return View::fetch('upload_file');
}
Once the form is submitted, we validate and store the uploaded file. Here's how to handle the uploaded file in ThinkPHP6:
public function importData()
{
$file = Request::file('file');
$savePath = 'public/uploads/';
$info = $file->validate([
'size' => 2048000,
'ext' => 'csv,xls,xlsx'
])->move($savePath);
if ($info) {
$filename = $info->getSaveName();
$filePath = $savePath . $filename;
// Proceed to import logic
} else {
// Handle upload failure
}
}
After a successful upload, the next step is to execute the actual MySQL import using LOAD DATA LOCAL INFILE.
use think\facade\Db;
public function importData()
{
// ...after upload logic
$filename = $info->getSaveName();
$filePath = $savePath . $filename;
$tableName = 'your_table_name'; // Replace with your actual table
$sql = "LOAD DATA LOCAL INFILE '{$filePath}' INTO TABLE `{$tableName}` FIELDS TERMINATED BY ','";
Db::connect()->execute($sql);
}
After the import, you may want to process the results further—such as logging the import or showing success messages.
public function importData()
{
// ...previous steps
$sql = "LOAD DATA LOCAL INFILE '{$filePath}' INTO TABLE `{$tableName}` FIELDS TERMINATED BY ','";
Db::connect()->execute($sql);
// Add optional post-import logic or logging here
}
By following these steps, you can efficiently import large datasets into MySQL using ThinkPHP6. With proper error handling and logging mechanisms, this solution becomes highly maintainable and scalable. It is ideal for scenarios such as e-commerce systems, content management platforms, and other data-intensive applications.