-1

We are working with a fingerprint scanner for attendance, it generates these kind of text files

1,MARCOS,MORENO,12:29,15:58,2020-02-26
2,ALEJANDRO,BASAL,12:05,18:01,2020-02-26
3,CARMEN,MORIA,11:58,17:59,2020-02-26

We want to upload it to our site, load the file, split that text into lines and then each line into arrays, to work with them, like so:

Array1 ([0] => 1 [1] => MARCOS [2] => MORENO [3] => 12:29 [4] => 15:58 [5] => 2020-02-26)
Array2 ([0] => 2 [1] => ALEJANDRO [2] => BASAL[3] => 12:05 [4] => 18:01 [5] => 2020-02-26)
Array3 ([0] => 3 [1] => CARMEN[2] => MORIA [3] => 11:58 [4] => 17:59 [5] => 2020-02-26)

To then load each array into a database. Each [0] going into id, [1] into name, etc. We managed to load and read the file, however we are unsure on how to split the file like we want to, so far our code only splits the full array without taking into account the line breaks, it returns this

Array ([0] => 1 [1] => MARCOS [2] => MORENO [3] => 12:29 [4] => 15:58 [5] => 2020-02-26 [6] => 2 [7] => ALEJANDRO [8] => BASAL[9] => 12:05 [10] => 18:01 [11] => 2020-02-26 [12] => 3 [13] => CARMEN[14] => MORIA [15] => 11:58 [16] => 17:59 [17] => 2020-02-26)

Each line is an employee and each line will always have the same amount of splits (by commas). Maybe if its not possible to split by line breaks just being able to handle additions will be enough (handle every [0]s with [6]s, [12]s, [18]s and so on together). To then update values in our database.

Our code right now is really simple, just

$file= 'attendance/' . '{file}';
$doc=file_get_contents($file);

$line=explode("\n",$doc);
foreach($line as $newline){
    echo '<h3 style="color:#453288">'.$newline.'</h3><br>';
}
  • try read file line by line using this https://stackoverflow.com/a/13246630/2177461 and each line you can use explode by comma , https://www.php.net/manual/en/function.explode.php – Krish Feb 27 '20 at 21:28
  • It's definitely possible to do this, but it's difficult to say what changes you need to make to your code to get it working the way you want without seeing it. – Don't Panic Feb 27 '20 at 21:29
  • Our code right now it's really simple, just $file = 'attendance/' . '{file}'; $doc=file_get_contents($file); $line=explode("\n",$doc); foreach($line as $newline){ echo '

    '.$newline.'


    '; }
    – Maximiliano Corbo Feb 27 '20 at 21:32

1 Answers1

0

try read file line by line using this https://stackoverflow.com/a/13246630/2177461 and each line you can use explode by comma , https://php.net/manual/en/function.explode.php


$file = 'attendance/' . '{file}'; 
$handle = fopen($file, "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
        $data = explode(',', $line);
        // $data will have Array1 ([0] => 1 [1] => MARCOS [2] => MORENO [3] => 12:29 [4] => 15:58 [5] => 2020-02-26)
        print_r($data);
    }

    fclose($handle);
} else {
    // error opening the file.
} 

Note: its pseudo code might have syntax errors

Krish
  • 387
  • 2
  • 13
  • Thank you, it does work, however for some reason the first two lines are combined and it just restarts counting from line three, maybe there's something wrong with the file? I'll check – Maximiliano Corbo Feb 27 '20 at 22:37