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!

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

15 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.

  7. Iman Syaefulloh Reply

    October 5, 2016 at 2:43

    Thanks, you just saved my day!

  8. Ulf Reply

    October 11, 2016 at 5:58

    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.

  9. Arash Reply

    December 18, 2016 at 11:33

    Great! I had spent hours in web with no success when I came here.
    Thank you!

  10. f4b1 Reply

    February 6, 2017 at 3:00

    I did not get away for several days with a similar bug … Your tip simply saved me, thank you very much!

  11. Bart Reply

    April 6, 2017 at 9:18

    Thanks, referenced this in:
    if you have more information avout this, please contribute 😉


  12. Matt Webley Reply

    February 7, 2018 at 10:49

    Sweet fix! I followed Bart here.. He was discussing this issue with some other guy
    Would like to point out you wrote this fix like 4 years ago and its it’s still a bug now!

  13. Tomasz Łaguna Reply

    May 30, 2018 at 4:09

    Oh my God! Thank You!
    I found link to Your article here: searching in Google by ‘laravel Content-type broken image’

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.