How To Fix Buffer Problem In Laravel Controllers

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.

laravel response image

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:

In your routes.php file;

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;

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!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

8 Comments on “How To Fix Buffer Problem In Laravel Controllers

  1. Maciek Reply

    November 11, 2014 at 1:22

    I wish that I did not find that a few hours earlier;)


  2. Agli Panci Reply

    April 2, 2015 at 7:47

    You just saved my day!

  3. Tommy Leirvik Reply

    June 11, 2015 at 10:44

    Brilliant, wich I found this post a few hours earlier!

  4. Carlos Vergara Reply

    June 14, 2015 at 12:12

    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.

  5. Kamil Reply

    February 24, 2016 at 7:13

    Thank U for this! 🙂

  6. Enrique Guizar Reply

    May 29, 2016 at 12:46

    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 Reply

      May 29, 2016 at 2:56

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *