The Response::download buffer problem is an issue that causes the output buffer to not be discarded and buffering is not turned off in PHP. Figuring out that you have this problem is not an easy task – Scott and I spent quite some time trying to find out why our images were not being rendered by our browser while working on a laravel project. That being said, we went to work – really went to town and tried to solve this issue. After a significant amount of caffeine, we found the solution – thanks in part to Scott’s relentless effort (I know he is reading this). So, here is the workaround.
The Problem Breakdown
First, we will consider the following scenario; I will make this as simple as possible because it should be just that.
In your controller, you have a function that is meant to return an image whenever you hit that endpoint through a route; Here is your controller – simplified for brevity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class GalleryController extends BaseController { //add all your functions here public function photo($id) { //make a query here to fetch the photo by id $photo_object = Photo::find($id); //since you want to ouput this photo to your browser, you set the header stuff $headers = array('Content-Type' => 'image/png'); //now create your new response here $response = \Response::download(storage_path($photo_object->filename), 'photo.png', $headers); //simply returning $response here will NOT work; return $response // just go get some coffee or even tea; } } |
In your routes.php file;
1 2 3 4 5 6 7 8 |
//You obviously have some route here Route::get('photo/{id}', array('as' => 'photo', 'uses' => 'GalleryController@photo')); Route::...... Route::...... even more routes for you here |
Attempting to hit that route in your browser will show a broken image – you know, those ugly things you see when an image is non-existent? It is now time to solve this issue and go get some sleep: it is easier than you think;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class GalleryController extends BaseController { //remember all your functions here; //now your photo function here again; public function photo($id) { //make a query here to fetch the photo by id $photo_object = Photo::find($id); //since you want to ouput this photo to your browser, you set the header stuff $headers = array('Content-Type' => 'image/png'); //now create your new response here $response = \Response::download(storage_path($photo_object->filename), 'photo.png', $headers); //HERE IS THE MAGIC FOLKS ob_end_clean(); //now this works like a charm; or whatever you like to call it; return $response } } |
Now your image will be valid and you can test it out by hitting the endpoint and opening the downloaded image.
Now, go discard the output buffer and turn off the buffering!
I hope the folks working on Laravel will fix this soon enough before they make grown men cry: ):
I hope this was helpful to you as it was to me. Thanks for reading and hope to see you again some time.
Please consider sharing this post with your friends using the buttons below if you found it helpful. See yah!
Maciek
I wish that I did not find that a few hours earlier;)
Thanks!!!
Agli Panci
You just saved my day!
Tommy Leirvik
Brilliant, wich I found this post a few hours earlier!
Carlos Vergara
Hey, I just wanna say THAAAANK YOU!!! I just spent all my day trying to solve this f*cking problem. I think, It was like 4 or 5 hours. I almost kill myself.Hehehe. It’s really magic your hack.
Keep the good work.
Kamil
Thank U for this! 🙂
Elisha Chirchir
Gladly!!
Enrique Guizar
For someone like me still learning laravel it would be impossible to figure this out without your help! The weird thing is that I made a new Laravel repo and the downloads work fine but on my old report (Updated from L5.1 to L5.2) I had to add ob_end_clean() as you suggested.
Enrique Guizar
I found the solution, the problem in my case was that there was a whitespace outside an opening <?php tag which was making my files corrupt. Once I found that in my config folder, there was no need to delete the output buffer.
Iman Syaefulloh
Thanks, you just saved my day!
Ulf
Thanks mate! Saved me hours of searching.
IMHO this should be part of Laravels default behaviour, the surplus newlines at the start of the output are just buggy, at least in the API route.
Arash
Great! I had spent hours in web with no success when I came here.
Thank you!
f4b1
I did not get away for several days with a similar bug … Your tip simply saved me, thank you very much!
Bart
Thanks, referenced this in: https://github.com/laravel/framework/issues/18687
if you have more information avout this, please contribute 😉
Bart
Matt Webley
Sweet fix! I followed Bart here.. He was discussing this issue with some other guy
https://github.com/Intervention/imagecache/issues/80
Would like to point out you wrote this fix like 4 years ago and its it’s still a bug now!
Tomasz Łaguna
Oh my God! Thank You!
I found link to Your article here: https://laravel.io/forum/02-22-2014-displaying-images-inline-text-and-broken-images-when-content-type-set searching in Google by ‘laravel Content-type broken image’