The issue is that you have globbed for all csv files, and whole.csv ends up at the bottom of that list.
When you fopen the whole file and read it line by line, but then append to it line by line, you are always writing ahead of where you're reading - infinite IO loop.
The solution without modifying your I/O code is to make sure whole.csv is removed from the globbed list:
Code:
$csvs = glob("*.csv");
/* New segment, removes whole.csv from the list of csv's to concatenate*/
foreach($csvs as $key => $file){
if($file == "whole.csv") unset($csvs[$key]);
}
/*End of new segment*/
$fp = fopen("whole.csv", 'w');
foreach($csvs as $csv) {
$row = 1;
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
fputcsv($fp, $data);
$row++;
}
fclose($handle);
}
}
I hope this helps!
Bookmarks