Example provoking a corrupt image to be download in a controller 4
public function downloadImage($post_id) {
    $post = Post::find($post_id);
    $file = public_path()."/uploads/Koala.jpg";
    $headers = array('Content-Type' => ' image/jpeg');
    return Response::download($file,'Koala.jpg',$headers);
}
If I remove the first line of the function it works. The only way I found to fix this issue was to modify the code this way:
public function downloadImage($post_id) {
    $post = Post::find($post_id);
    $file = public_path()."/uploads/Koala.jpg";
    $headers = array('Content-Type' => ' image/jpeg');
    $response = Response::download($file,'Koala.jpg',$headers);
    ob_end_clean();
    return $response;
}
by გიორგი უზნაძე
3 years ago
PHP
Laravel
File
Download
Images
0
Pro tip: use ```triple backticks around text``` to write in code fences