json-csv
json-csv copied to clipboard
Adds Content-length Header For Downloads
When downloading files it is useful to send the content length header so that the browser can give the user some indication of progress, especially with large files
Great idea, but I think it is not correctly implemented as filename is a string. Something like this would be better, but I have not tested it.
public function convertAndDownload(?string $filename = null, bool $exit = true): void
{
$filename = $filename ?? $this->filename;
$convertedData = $this->convert();
$this->sendHeaders($filename, $convertedData);
echo $convertedData;
if ($exit === true) {
exit();
}
}
protected function sendHeaders(string $filename, string $data): void
{
header('Content-disposition: attachment; filename=' . $filename . '.' . $this->conversion['extension']);
header('Content-type: ' . $this->conversion['type']);
header('Content-length: ' . strlen($data)); // Use strlen() on the actual data
}