-2

I have a issue. In my original code I opened a file with:

$file = fopen ("files/bla1.exp" , "r" );

And after opening the file, I want to read the CSV line by line with:

$aCsvLine = fgetcsv($file,"0",";",'"');

That works fine, but now the problem. Now I want to open multiple files and then read line by line with fgetcsv.

This is nog working:

if($sDate ==  "2022-07-18" || $sDate ==  "2022-07-19" || $sDate ==  "2022-07-20" || $sDate ==  "2022-07-21" || $sDate ==  "2022-07-22")
      {
        $file = fopen ("files/bla1.exp" , "r" );
        $file = fopen ("files/bla2.exp" , "r" );
        $file = fopen ("files/bla3.exp" , "r" );
      }

And this from my other post is also not working, because of missing the handle:

    $file = '';
if ($sDate ==  "2022-07-18" || $sDate ==  "2022-07-19" || $sDate ==  "2022-07-20" || $sDate ==  "2022-07-21" || $sDate ==  "2022-07-22")
      {
        $file .= file_get_contents("files/bla1.exp");
        $file .= file_get_contents("files/bla2.exp");
        $file .= file_get_contents("files/bla3.exp");
      }

How can I first open my csv files and append them to one file and the read it line by line. I hope I made my question clear.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Use different variables (`$file1`, `$file2`, etc) or an array (`$files = [ fopen(), fopen(), ...]`) to store your file handles – Phil Jun 21 '22 at 23:24
  • I need to open it all as one big file together and then read line by line. Normally I copied the files by hand into one file. But that takes to long. I am now looking for a dynamic way. – mrdigitalis Jun 21 '22 at 23:38
  • Looking at this question along with your previous post it's far from clear what you're trying to do. You seem to have a number of CSV files which you're trying to read and extract the CSV data from. You haven't said what you're doing with the CSV data when you have it. This question is addressing problems arising from the answer to your last question, but in reality, you're not asking about the whole problem, so the answers won't necessarily work with the problem you've got. – Tangentially Perpendicular Jun 21 '22 at 23:40
  • 1
    _"I need to open it all as one big file together"_ - why? Why will opening the files sequentially not work? What are you doing with the data once you've extracted it? – Tangentially Perpendicular Jun 21 '22 at 23:42
  • The file contains duty’s from linebusses. Drivers can search for their duty’s. They search for a dutynumber on a certain date. And the data is formatted displayed in their phones. – mrdigitalis Jun 21 '22 at 23:46
  • 1
    It seems to me that doing this with a bunch of CSV files is not the right approach. Have you considered using a database for this? – Tangentially Perpendicular Jun 22 '22 at 01:28
  • @Tangentially Perpendicular You are right, that will be the next step. But I do not know how to model and structure the data in the database so that all added files are unique and recognisable to find with queries. I am not that far in programming. – mrdigitalis Jun 22 '22 at 19:23
  • @phil I am going to experiment this weekend with the provided code. Thanks! – mrdigitalis Jun 22 '22 at 19:28

1 Answers1

1

Create an array of filenames and iterate that. In each iteration, open the file and start reading from it using fgetcsv().

$files = ['files/bla1.exp', 'files/bla2.exp', 'files/bla3.exp'];
$allRows = [];

foreach ($files as $file) {
    $fh = fopen($file, 'rb');
    while (($row = fgetcsv($fh, 0, ';', '"')) !== false) {
        $allRows[] = $row; // ¯\_(ツ)_/¯
    }
    fclose($fh);
}
Phil
  • 157,677
  • 23
  • 242
  • 245