<?php
/**
* Restores data from text files to a database.
*
* This script assumes a simple text file format where each line represents a record.
* The first line of each record is treated as the column headers.
*
* @param string $input_dir The directory containing the text files.
* @param string $db_host The database host.
* @param string $db_name The database name.
* @param string $db_user The database user.
* @param string $db_pass The database password.
* @param string $table_name The table name to import data into.
* @return void
*/
function restoreDataFromTextFiles(string $input_dir, string $db_host, string $db_name, string $db_user, string $db_pass, string $table_name): void
{
if (!is_dir($input_dir)) {
echo "Error: Input directory '$input_dir' does not exist.\n";
return;
}
$files = scandir($input_dir);
if ($files === false) {
echo "Error: Could not scan the directory.\n";
return;
}
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
if (pathinfo($file, PATHINFO_EXTENSION) !== 'txt') {
continue;
}
$filepath = $input_dir . '/' . $file;
if (!file_exists($filepath)) {
echo "Warning: File '$filepath' not found.\n";
continue;
}
try {
$data = parseTextFile($filepath);
if ($data === false) {
echo "Error: Failed to parse file '$filepath'.\n";
continue;
}
insertDataIntoDatabase($db_host, $db_name, $db_user, $db_pass, $table_name, $data);
} catch (Exception $e) {
echo "Error processing file '$filepath': " . $e->getMessage() . "\n";
}
}
}
/**
* Parses a text file into an array of associative arrays.
* The first line is treated as column headers.
*
* @param string $filepath The path to the text file.
* @return array|false An array of associative arrays, or false on error.
*/
function parseTextFile(string $filepath): array|false
{
$file = fopen($filepath, 'r');
if ($file === false) {
return false;
}
$headers = fgetcsv($file);
if ($headers === false) {
fclose($file);
return false;
}
$data = [];
while (($row = fgetcsv($file)) !== false) {
if (count($row) !== count($headers)) {
echo "Warning: Row in '$filepath' has incorrect number of columns. Skipping row.\n";
continue;
}
$data[] = array_combine($headers, $row);
}
fclose($file);
return $data;
}
/**
* Inserts data into the database.
*
* @param string $db_host The database host.
* @param string $db_name The database name.
* @param string $db_user The database user.
* @param string $db_pass The database password.
* @param string $table_name The table name.
* @param array $data An array of associative arrays to insert.
* @return void
*/
function insertDataIntoDatabase(string $db_host, string $db_name, string $db_user, string $db_pass, string $table_name, array $data): void
{
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Enable error reporting
$stmt = $pdo->prepare("INSERT INTO $table_name (");
$keys = array_keys($data[0]);
$stmt->format($keys);
$stmt->execute();
foreach ($data
Add your comment