E
E
evgenin2019-03-15 00:49:53
csv
evgenin, 2019-03-15 00:49:53

How to read the first line of a large CSV file?

if (($handle = fopen($filename, 'r')) !== FALSE) {
          $rowNumber = 0;
          $output = array();

          while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
              $num = count($row);

              for ($i = 0; $i < $num; $i++) {
                  $output[$rowNumber][$i] = $row[$i];
              }
              $rowNumber++;
          }
        }

error on line while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 72 bytes)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
evgenin, 2019-03-15
@evgenin

private function part($path, $offset, $rows) {
    $buffer = array();
    $buffer['content'] = '';
    //$buffer['pointer'] = array();
    $handle = fopen($path, "r");
    fseek($handle, $offset);
    if( $handle ) {
      for( $i = 0; $i < $rows; $i++ ) {
        $buffer['content'] .= fgets($handle);
        //$buffer['pointer'] = mb_strlen($buffer['content']);
      }
    }
    fclose($handle);
    return($buffer);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question