Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Upload large files to S3 using Laravel 5

Chris Blackwell yesterday published a tutorial on how to upload files to S3 using Laravel.

This is the code he used (slightly redacted):


$disk= Storage::disk('s3');
$disk->put($targetFile, file_get_contents($sourceFile));

This is a good way to go about it for small files. You should note that file_get_contents will load the entire file into memory before sending it to S3. This can be problematic for large files.

If you want to upload big files you should use streams. Here’s the code to do it:


$disk = Storage::disk('s3');
$disk->put($targetFile, fopen($sourceFile, 'r+'));

PHP will only require a few MB of RAM even if you upload a file of several GB.

You can also use streams to download a file from S3 to the local file system:


$disk = Storage::disk('s3');
$stream = $disk->getDriver()
->readStream($sourceFileOnS3);
file_put_contents($targetFile, stream_get_contents($stream), FILE_APPEND);

You can even use streams to copy file from one disk to another without touching the local filesystem:


$stream = Storage::disk('s3')->getDriver()
->readStream($sourceFile);

Storage::disk('sftp')->put($targetFile, $stream);

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • ivacuum

    You could use

    $disk->put($targetFile, fopen($sourceFile, ‘r+’));

    right away instead of getDriver()->putStream() chain.

    http://laravel.com/docs/5.1/filesystem#storing-files

    • Thanks, I didn’t know that. Updated my post with this short and sweet syntax.

  • Do I still have to close the stream with fclose or does the upload handle that for me?

  • Hendrik Grobler

    Any chance you could show you would get upload progress?

  • Ewan Valentine

    Just what I was looking for! Thanks!

  • AbrahamCruz

    Is it possible to pipe one file while it is being uploaded?

  • Saul Burgos

    Hi when I use : $disk->put($targetFile, fopen($sourceFile, ‘r+’));

    The images uploaded with this method are downloaded instead loaded in the broswer. I guess is about the mimetype right ?

    How can I resolve this ?

    • $disk()>getDriver()->put($destination, fopen($sourcefile, ‘r+’), [‘ContentType’ => ‘your mime type’]);

      • Saul Burgos

        Thanks ! 🙂

  • Pingback: ? This blog is one year old ? | murze.be()

  • Harvey Vega

    Hi Chris.

    Thanks, I’m using your code lines for files of 500mb with jquery-upload-file. My problem is that the bar progress don’t work with it, do you know the way to get it work?

    Thanks again!

  • Kieran

    If anyone is having issues uploading / downloading, here is the code i ussed

    /**
    * @param $id
    * @return SymfonyComponentHttpFoundationBinaryFileResponse
    */
    public function downloadFile($id)
    {

    $file = Plan::findOrFail($id);

    // Check if it’s stored locally, from original upload
    if(substr($file->path, 0, 3) === ‘app’)
    {
    return response()->download(storage_path(“{$file->path}”), “{$file->name}.pdf”);
    }

    $disk = Storage::disk(‘s3’);
    $cloudFile = $disk->getDriver()->readStream($file->path);
    $size = $disk->size($file->path);
    $mimeType = $disk->mimeType($file->path);

    return Response::stream(function() use($cloudFile, $size, $mimeType, $file) {
    fpassthru($cloudFile);
    }, 200, [
    “Content-Type” => $mimeType,
    “Content-Length” => $size,
    “Content-disposition” => “attachment; filename={$file->name}”,
    ]);

    }

    public function uploadFile(Request $request)
    {
    //Use Plupload for multiple uploads and larger files
    return Plupload::receive(‘file’, function ($file) use ($request)
    {

    $s3 = Storage::disk(‘s3’);

    $name = sha1(time() . $file->getClientOriginalName());
    $extension = $file->getClientOriginalExtension();
    $fileName = “{$name}.{$extension}”;

    $filePath = “plans/$request->buildingName/$fileName”;

    $s3->put($filePath, fopen($file, ‘r+’));

    Plan::create([
    ‘building_id’ => htmlspecialchars_decode($request->building_id),
    ‘name’ => $file->getClientOriginalName(),
    ‘filename’ => $fileName,
    ‘path’ => $filePath,
    ‘file_type’ => $file->getClientOriginalExtension(),
    ‘floor_id’ => null,
    ‘type_id’ => null
    ]);

    return $s3->url($filePath);
    });

    }

  • Luca

    What about request timeout?