Skip to content

Instantly share code, notes, and snippets.

@kcassam
Last active December 25, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcassam/6901793 to your computer and use it in GitHub Desktop.
Save kcassam/6901793 to your computer and use it in GitHub Desktop.
Aggregate multiple files in one (Quick and dirty)
<?php
$pathname = __DIR__."/filestoaggregate";
$tab[0] = array("firstname","lastname","gender","email","code","newsletter","variable","giftcode");
$separator = "=";
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);
$i = 1;
foreach($iterator as $item) {
$j = 0;
if (in_array(basename($item), array(".", "..", ".DS_Store"))) {
continue;
}
$fic=fopen($item, "r");
while(!feof($fic))
{
$ligne= fgets($fic,1024);
$ligne = str_replace(CHR(13).CHR(10),"",$ligne);
$pos = strpos($ligne, "user");
if($pos == false){
$array = explode($separator, $ligne);
$chaine = str_replace('"', '', $array[1]);
$tab[$i][$j] = $chaine;
}
$j++;
}
$i++;
fclose($fic) ;
}
$fp = fopen('output.csv', 'w');
foreach ($tab as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment