Download from Laravel storage without loading whole file in memory

后端 未结 6 762
渐次进展
渐次进展 2020-12-13 06:43

I am using Laravel Storage and I want to serve users some (larger than memory limit) files. My code was inspired from a post in SO and it goes like this:

$fs         


        
6条回答
  •  轮回少年
    2020-12-13 07:01

    X-Send-File.

    X-Send-File is an internal directive that has variants for Apache, nginx, and lighthttpd. It allows you to completely skip distributing a file through PHP and is an instruction that tells the webserver what to send as a response instead of the actual response from the FastCGI.

    I've dealt with this before on a personal project and if you want to see the sum of my work, you can access it here:
    https://github.com/infinity-next/infinity-next/blob/master/app/Http/Controllers/Content/ImageController.php#L250-L450

    This deals not only with distributing files, but handling streaming media seeking. You are free to use that code.

    Here is the official nginx documentation on X-Send-File.
    https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/

    You do have to edit your webserver and mark specific directories as internal for nginx to comply with X-Send-File directives.

    I have example configuration for both Apache and nginx for my above code here.
    https://github.com/infinity-next/infinity-next/wiki/Installation

    This has been tested on high-traffic websites. Do not buffer media through a PHP Daemon unless your site has next to no traffic or you're bleeding resources.

提交回复
热议问题