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

Memory exhausted error when downloading large assets #2401

anakadote opened this issue May 21, 2019 · 0 comments


None yet
3 participants
Copy link

commented May 21, 2019

When attempting to download a very large file via \Statamic\Http\Controllers\AssetContainer:download() a php memory exhaustion error is thrown, e.g.:

Symfony\Component\Debug\Exception\FatalErrorException: Allowed memory size of 536870912 bytes exhausted (tried to allocate 584040448 bytes) in ...

Replacing the call to passthru() on line 324 with the following resolves the issue for me:

while (! feof($stream)) {
    echo fread($stream, 8192);

Full updated method code:

public function download($container_id, $path)
    $container = AssetContainer::find($container_id);
    $asset = $container->asset($path);

    $file = $asset->path();

    $filesystem = $asset->disk()->filesystem()->getDriver();
    $stream = $filesystem->readStream($file);

    return response()->stream(function () use ($stream) {
        while (! feof($stream)) {
            echo fread($stream, 8192);
    }, 200, [
        "Content-Type" => $filesystem->getMimetype($file),
        "Content-Length" => $filesystem->getSize($file),
        "Content-disposition" => "attachment; filename=\"" . basename($file) . "\"",

@jesseleite jesseleite self-assigned this Jun 27, 2019

@jackmcdade jackmcdade closed this Jun 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.