0

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

1 Answers1

0

I think this question has already been answered here in SO. All you have to do is to loop each uploaded file.

<?php 
$total = count($_FILES['file']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {
  // Get the temp file path
  $tmpFilePath = $_FILES['file']['tmp_name'][$i];

    //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./upload/" . $_FILES['file']['name'][$i];
    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {
      //Handle other code here
      if (($fichier = fopen("upload/".$_FILES['file']['name'][$i], "r")) !== FALSE) {
        $oui_total = 0;
        $non_total = 0;

        $tab_oui = array();
        $tab_non = array();
        $tab_sat = array();

        $tableau = array();

        // more code below...
        // ...
      }
    }
  }
}

Check this link to give you more understanding:
Multiple file upload in php
http://php.net/manual/en/features.file-upload.multiple.php

Hope it helps..

Arman Ortega
  • 3,003
  • 1
  • 30
  • 28
  • Thanks for your answer ! I did try that but my data for one file has to be table for several file right ? – CamilleKah Jun 01 '17 at 09:59
  • Sorry about that, I didn't finish taping but I press Enter to got to the line and the comment did post, so can you look my edited first comment please ? :) – CamilleKah Jun 01 '17 at 10:06