Home » Php » Laravel File Downloaded from storage folder gets corrupted

Laravel File Downloaded from storage folder gets corrupted

Posted by: admin December 20, 2017 Leave a comment

Questions:

I tried to download jpg/png image from storage folder its gets corrupted after downloaded.
This is my controller

public function download($filename) {

    $headers = array(
        'Content-Type: image/png',
    );
    return response()->download(storage_path() . '/'.$filename, 'final.png', $headers);

}

after open it look like this
enter image description here

Even i used core php script to download still iam facing same problem.

Answers:

I believe that the Laravel framework might be introducing whitespace which might be ruining the header() function.

Use ob_end_clean() before your first header() call to remove any extra whitespace.

Questions:
Answers:

add this method before like this

ob_end_clean();
$headers = array(
    'Content-Type: image/png',
);
return response()->download(storage_path() . '/'.$filename, 'final.png', $headers);

!enjoy