Yes, you can use glob
to fetch a list of files in your directory, then loop through them with foreach()
and use your existing code.
<?php
$arrFiles = glob("../*.csv");
foreach($arrFiles as $strFileName) {
$file_handle = fopen($strFileName, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>';
}
fclose($file_handle);
}
You might want to limit the amount of files you open and close rather than opening all files in that directory - or cache the results.
To limit the amount of files you open, you can use array_slice
;
$arrFiles = glob("../*.csv");
$arrFiles = array_slice($arrFiles, 0, 10); //Limit to 10 files
Sorting output based on date modified
What you need to do is grab a list of files, then get their last modified time, then sort that array. For example:
$arrFiles = glob("../*.csv");
$arrSortedFiles = array();
foreach($arrFiles as $strFileName) {
$arrSortedFiles[$strFileName] = filemtime($strFileName);
}
natsort($arrSortedFiles); //Sort naturally. Oldest first.
foreach(array_keys($arrSortedFiles) as $strFileName) {
//...
}
If, however, you want the sort the other way around - newest first - then just use array_reverse()
.