sarincm99
sarincm99

Reputation: 123

how to traverse through multiple csv files in php

I have a form,i want to take value from form and check this value to the value in csv file. But it can be in any of the multiple csv files.

So how can i traverse through multiple csv files

For example, I have 50 csv files i want to traverse through these 50 csv files until i get the appropriate result.

I know how to traverse through 1 csv file code i used for this is written below

<?PHP
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
    $line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'test.csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>

Like this how can i traverse through multiple csv files.

Upvotes: 1

Views: 501

Answers (1)

Raptor
Raptor

Reputation: 54212

Here is an example:

$files = glob('/path/to/dir/*.csv');
foreach($files as $f) {
  $csv = readCSV($f);
  echo '<pre>';
  print_r($csv);
  echo '</pre>';
}

Reference: glob() - function to list files in specific directory with specific pattern

Upvotes: 1

Related Questions