First of all, I'm french so excuse my English please ! I've got a problem that I can't resolve. I've got a web site that allow me to drag-and-drop multiple CSV file, but only process one file at a time. It works perfectly with 1 file, it reads data in the file and display an High-charts graph. I'd like to display several graph which came from several CSV file, all written the same way. I manage to do a drag-and-drop which can upload several file at a time, and it works besauce I get this :
Array ( [name] => Array ( [0] => 14-04-2014 (1).csv [1] => 14-04-2014.csv ) [type] => Array ( [0] => text/csv [1] => text/csv ) [tmp_name] => Array ( [0] => /tmp/phpbQZ7NI [1] => /tmp/phptvEwXI ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 589 [1] => 589 ) )
On the other hand, I can't manage to process those files in a loop.
Here is the code for one file, without a loop :
if (($fichier = fopen("upload/".$_FILES['file']['name'], "r")) !== FALSE) {
$oui_total = 0;
$non_total = 0;
$tab_oui = array();
$tab_non = array();
$tab_sat = array();
$tableau = array();
$date = null;
$id_borne= null;
while (($data = fgetcsv($fichier,0, ",")) !== FALSE) { //on lit ligne par ligne dans cette boucle !
//on lit l'ID de la borne s'il existe
if ((mb_strimwidth($data[0], 0, 4,'') != "2014")
AND (mb_strimwidth($data[0], 0, 4,'') != "2015")
AND (mb_strimwidth($data[0], 0, 4,'') != "2016")
AND (mb_strimwidth($data[0], 0, 4,'') != "2017")
AND (mb_strimwidth($data[0], 0, 4,'') != "2018")
AND (mb_strimwidth($data[0], 0, 4,'') != "2019"))
{
$id_borne=$data[0];
}
elseif ($date === null) {
$date = mb_strimwidth($data[0], 0, 10,'');
}
// sauter les lignes vides
if ($data[0] === null) {
continue;
}
// pour écrire le sous-titre du graphe
if (strlen($data[O]) > 5 && $date === null)
// parser l'heure et la date
$data[0] = trim($data[0]);
$format_date = preg_match(
'~
(?P<year> \d+) -
(?P<month> \d+) -
(?P<day> \d+) \h+
(?P<hour> \d+) :
(?P<minute> \d+) :
(?P<second> \d+)
~x',
$data[0],
$match
);
//if ($format_date == false) {
// exit("Le fichier csv n'a pas le format attendu: la date devrait être \"yyyy-mm-dd hh:mm:ss\".");
//}
// Par défaut : heure d'hiver. Donc on a les bons horaires du 25 oct au 29 mars.
// ete : 29 mars et +
// hiver : 25 octobre et +
// '$ete' vaut 'true' si la date du CSV est en été
$ete = ( (intval($match['month']) > 3 && intval($match['month']) < 10)
|| (intval($match['month']) === 3 && intval($match['day']) > 28)
|| (intval($match['month']) === 10 && intval($match['day']) < 25) );
$correctif_heure = $ete === true ? 1 : 0;
// on stocke l'information dans un tableau avec l'heure en minutes pour clef (exemple: 8*60+15=495 pour 8h15)
$heure_en_minute = (intval($match['hour']) + $correctif_heure) * 60 + intval($match['minute']);
$tableau[$heure_en_minute] = array(
'oui' => intval($data[1]),
'non' => intval($data[2]),
'sat' => ceil($data[1] / ($data[1] + $data[2]) * 100)
);
$oui_total += $data[1];
$non_total += $data[2];
}
fclose($fichier);
}
else {
exit(TXT_IMPOSSIBLE);
}
unlink($dossier . $_FILES['file']['name']);
$total = $oui_total + $non_total;
$sat_total = ceil($oui_total / $total * 100);
$unsat_total = 100 - $sat_total;
Despite all of my attempts, I can manage to "transform" this code for it to read multiple file at a time. Any help will be appreciated ! Thanks