I am trying to send a tar.gz file from Laravel as a response, but it is not working properly. What is the error in the code? Here $filepath is the absolute path to the tar.gz file.
if(file_exists($filepath)) {
$file = File::get($filepath);
$response = Response::make($file, 200);
$response->header('Content-Type', mime_content_type($filepath));
$response->header('Content-Length', filesize($filepath));
$response->header('Pragma', 'public');
$response->header('Content-Disposition', 'attachment; filename="sample.tar.gz"');
return $response;
}
else {
return $this->response->array(array('error' => 'Could not be downloaded, try again'));
}
CodePudding user response:
I found a simpler approach using raw PHP which works perfectly.
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="sample.tar.gz"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush();
readfile($filepath);
exit();
}