Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloading large files gives out of memory errors #31159

Closed
sebsel opened this issue Jan 17, 2020 · 5 comments
Closed

Downloading large files gives out of memory errors #31159

sebsel opened this issue Jan 17, 2020 · 5 comments
Labels

Comments

@sebsel
Copy link
Contributor

sebsel commented Jan 17, 2020

  • Laravel Version: 6.5.2
  • PHP Version: 7.4.0

Description:

This bug manifested itself when I pointed Laravel Nova's File field to a file of 1,04 GB. Nova is unable to download those files. The log shows an out-of-memory error.

I investigated the problem and I think it lies with Illuminate\Filesystem\FilesystemAdapter, which uses a fpasstru() of the stream.

$response->setCallback(function () use ($path) {
$stream = $this->readStream($path);
fpassthru($stream);
fclose($stream);
});

This loads the whole file in memory. The following would work:

        $response->setCallback(function () use ($path) {
            $stream = $this->readStream($path);
            while (! feof($stream)) {
                echo fread($stream, 2048);
            }
            fclose($stream);
        });

Steps To Reproduce:

Get yourself a large file and point a tinker session to it:

>>> Storage::download('large-file.csv')->send()

With fpassthru(), you get a "Allowed memory size of 134217728 bytes exhausted (tried to allocate 1041674240 bytes)".
With the while loop it just streams data.

Happy to PR.


Credits: https://www.php.net/manual/en/function.fpassthru.php#29162
More examples on that page too.

@taylorotwell
Copy link
Member

Feel free to send in a PR. Thanks.

@driesvints
Copy link
Member

PR was merged.

@cameronjohnson-mz
Copy link

cameronjohnson-mz commented Dec 14, 2022

@driesvints how come this change isn't in Laravel 9.x? How do I apply this fix to my vendor files?

https://github.com/laravel/framework/blob/9.x/src/Illuminate/Filesystem/FilesystemAdapter.php#L298

@sebsel
Copy link
Contributor Author

sebsel commented Dec 15, 2022

Because it was reverted, see the PR: #31163 (comment)

@HDVinnie
Copy link
Contributor

This is still an issue....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants