Skip to content

Commit 128e541

Browse files
committed
Improve AbstractCsv::download implementation #569
1 parent e0b91a9 commit 128e541

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/AbstractCsv.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,25 @@ public function download(?string $filename = null): int
258258
HttpHeaders::forFileDownload($filename, 'text/csv');
259259
}
260260

261-
$bomOutputLength = 0;
261+
$bytes = 0;
262+
$output = new SplFileObject('php://output', 'wb');
262263
if (null !== $this->output_bom) {
263-
echo $this->output_bom->value;
264-
$bomOutputLength = $this->output_bom->length();
264+
$bytes += $output->fwrite($this->output_bom->value);
265265
}
266266

267267
$this->getInputBOM();
268268
$this->document->rewind();
269269
$this->document->setFlags(0);
270270
$this->is_input_bom_included || -1 < $this->document->fseek($this->input_bom?->length() ?? 0) || throw new RuntimeException('Unable to seek the document.');
271271

272-
$documentOutputLength = 0;
273272
while (!$this->document->eof()) {
274273
$chunk = $this->document->fread(8192);
275274
false !== $chunk || throw new RuntimeException('Unable to read the document.');
276-
$documentOutputLength += strlen($chunk);
277-
echo $chunk;
278-
flush();
275+
$bytes += $output->fwrite($chunk);
276+
$output->fflush();
279277
}
280278

281-
return $bomOutputLength + $documentOutputLength;
279+
return $bytes;
282280
}
283281

284282
/**

0 commit comments

Comments
 (0)