1. <?php
  2. /**
  3. * Restores data from text files to a database.
  4. *
  5. * This script assumes a simple text file format where each line represents a record.
  6. * The first line of each record is treated as the column headers.
  7. *
  8. * @param string $input_dir The directory containing the text files.
  9. * @param string $db_host The database host.
  10. * @param string $db_name The database name.
  11. * @param string $db_user The database user.
  12. * @param string $db_pass The database password.
  13. * @param string $table_name The table name to import data into.
  14. * @return void
  15. */
  16. function restoreDataFromTextFiles(string $input_dir, string $db_host, string $db_name, string $db_user, string $db_pass, string $table_name): void
  17. {
  18. if (!is_dir($input_dir)) {
  19. echo "Error: Input directory '$input_dir' does not exist.\n";
  20. return;
  21. }
  22. $files = scandir($input_dir);
  23. if ($files === false) {
  24. echo "Error: Could not scan the directory.\n";
  25. return;
  26. }
  27. foreach ($files as $file) {
  28. if ($file === '.' || $file === '..') {
  29. continue;
  30. }
  31. if (pathinfo($file, PATHINFO_EXTENSION) !== 'txt') {
  32. continue;
  33. }
  34. $filepath = $input_dir . '/' . $file;
  35. if (!file_exists($filepath)) {
  36. echo "Warning: File '$filepath' not found.\n";
  37. continue;
  38. }
  39. try {
  40. $data = parseTextFile($filepath);
  41. if ($data === false) {
  42. echo "Error: Failed to parse file '$filepath'.\n";
  43. continue;
  44. }
  45. insertDataIntoDatabase($db_host, $db_name, $db_user, $db_pass, $table_name, $data);
  46. } catch (Exception $e) {
  47. echo "Error processing file '$filepath': " . $e->getMessage() . "\n";
  48. }
  49. }
  50. }
  51. /**
  52. * Parses a text file into an array of associative arrays.
  53. * The first line is treated as column headers.
  54. *
  55. * @param string $filepath The path to the text file.
  56. * @return array|false An array of associative arrays, or false on error.
  57. */
  58. function parseTextFile(string $filepath): array|false
  59. {
  60. $file = fopen($filepath, 'r');
  61. if ($file === false) {
  62. return false;
  63. }
  64. $headers = fgetcsv($file);
  65. if ($headers === false) {
  66. fclose($file);
  67. return false;
  68. }
  69. $data = [];
  70. while (($row = fgetcsv($file)) !== false) {
  71. if (count($row) !== count($headers)) {
  72. echo "Warning: Row in '$filepath' has incorrect number of columns. Skipping row.\n";
  73. continue;
  74. }
  75. $data[] = array_combine($headers, $row);
  76. }
  77. fclose($file);
  78. return $data;
  79. }
  80. /**
  81. * Inserts data into the database.
  82. *
  83. * @param string $db_host The database host.
  84. * @param string $db_name The database name.
  85. * @param string $db_user The database user.
  86. * @param string $db_pass The database password.
  87. * @param string $table_name The table name.
  88. * @param array $data An array of associative arrays to insert.
  89. * @return void
  90. */
  91. function insertDataIntoDatabase(string $db_host, string $db_name, string $db_user, string $db_pass, string $table_name, array $data): void
  92. {
  93. try {
  94. $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  95. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Enable error reporting
  96. $stmt = $pdo->prepare("INSERT INTO $table_name (");
  97. $keys = array_keys($data[0]);
  98. $stmt->format($keys);
  99. $stmt->execute();
  100. foreach ($data

Add your comment