0

I'm very rookie in PHP and when I combine several csv files with this code, several headers are shown. How could I leave a single header?

I am using a ubuntu 18.04 server with PHP 7.1

<?php

$csvdir = "./csv";
$csvcontent = '';
if (is_dir($csvdir)) {
    if ($handle = opendir($csvdir)) {
        while (($file = readdir($handle)) !== false) {
            if (substr($file, -4) === ".csv") {
                $csvcontent .= file_get_contents($csvdir . $file);
            }
        }
        closedir($handle);
    }
}

$result = fopen('./todos.csv', 'w');
fwrite($result, $csvcontent);
fclose($result);
?>
Marc
  • 3
  • 2

2 Answers2

0

This has the added benefit of not requiring you to hold the entirety of the resulting file in memory:

$files = []; // use your existing code to build a list of files

$first = true;
$out = fopen('todos.csv', 'wb');

foreach( $files as $file ) {
  $in = fopen($file, 'rb');

  $line = fread($in); // get the header line

  // write only the first file's header
  if( $first ) {
    fwrite($out, $line);
    $first = false;
  }

  // transcribe the rest of the file.
  while( $line = fread($in) ) {
    fwrite($out, $line);
  }

  fclose($in);
}

fclose($out);
Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

According to this answer with example code to skip first lines after getting file contents:

$content = file_get_contents($filename);
$lines = explode("\n", $content);
$skipped_content = implode("\n", array_slice($lines, 2));

You might alter your code like this (although, personally, i might simply alter the production of the csv files to stop producing headers...as exploding the content could slow down your processes).

<?php

$got_headers = 0;
$csvdir = "./csv";
$csvcontent = '';
if (is_dir($csvdir)) {
  if ($handle = opendir($csvdir)) {
    while (($file = readdir($handle)) !== false) {
      if (substr($file, -4) === ".csv") {
        if (empty($got_headers)) {

          // get the headers the first time only
          $csvcontent .= file_get_contents($csvdir . $file);
          $got_headers = 1;
        } else {

          // now, pull the lines after the first
          $content = file_get_contents($csvdir . $file);
          $lines = explode("\n", $content);

          $csvcontent .= implode("\n", array_slice($lines, 1));
        }
      }
    }
    closedir($handle);
  }
}

$result = fopen('./todos.csv', 'w');
fwrite($result, $csvcontent);
fclose($result);
WEBjuju
  • 5,797
  • 4
  • 27
  • 36