The following method should allow you to load all of the data into your DB while keeping only one file's contents in the PHP memory at a time.
You can write a new CSV in PHP with something similar to:
$newCsvPath = '/path/to/where/you/want/to/save/csv';
$newCsvFileName = 'newcsv.csv';
$newCsvFullFileName = $newCsvPath.$newCsvFileName;
$newCSV = fopen($newCsvFullFileName,'w+');
$dir = '/directory/for/your/text/files';
$fileArray = array_diff(scandir($dir), array('.', '..'));
$fileCount = count($fileArray);
if ($fileCount > 0) {
foreach ($fileArray as $file) {
$filename = $dir . "/" . $file;
$f = fopen($filename,'r');
$added = new Date();
$added = date_format($added,'Y-m-d');
$text = file_get_contents($f);
$row = $file . "," . $added . "," . $text;
fputcsv($newCSV,$row,",","\"");
//you might want to remove the last "enclosure" parameter above depending on your preference
}
}
Then, you can load the newly created CSV into the DB using this SQL (via whatever PHP MySQL tool you use):
LOAD DATA LOCAL INFILE $newCsvFullFileName
IGNORE INTO TABLE temp_table
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
IGNORE 1 LINES;
Obviously you would need to modify the above LOAD DATA statement for your particular needs (i.e. how the fields are delimited/enclosed, if there are field names in the first row, etc.)
You also might have to modify the above $newCsvFullFileName
by adding escaping backslashes for the SQL statement to work.