For a recent project, I needed to create a large batch of files for the user to download all at once. Creating a zip/tar was of course the solution. However, the file was always corrupted when downloaded and opened, but it was fine when read on the server.
It turns out that PHP was outputting some extra data before readfile() was called, so, I needed to clean the output buffer first, using ob_clean();
Example:
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=file.tar.gz");
header('Content-Length: ' . filesize('/tmp/file.tar.gz'));
ob_clean();
readfile('/tmp/file.tar.gz');
exit();

Comments
Post new comment