michaeldzjap's avatar

Storing uploads locally with Storage class error

I'm trying to store an uploaded file using the Storage class, but for some reason only a small part of the file is actually stored (14kb always) and I just can't figure out why...

I created an UploadsManager class which has the following constructor and saveFile function:

<?php

namespace App\Services;

use Illuminate\Support\Facades\Storage;

class UploadsManager
{
    protected $disk;

    public function __construct()
    {
        $this->disk = Storage::disk('local');    
    }

    ...

    public function saveFile($path, $content)
    {
        return $this->disk->put($path, $content);
    }
}

I verified that the $path and $content variables contain sane data just before the call to $this->disk->put by var_dump'ing them and they most definitely do (i.e. the file size, mime type, file name etc. looks exactly like what I expect it should be). To make it even stranger; replacing the line

return $this->disk->put($path, $content);

with

return $content->move(base_path() . '/public/uploads', $path);

(hence bypassing the Storage class altogether) does store the whole file at the right location.

Even more, it did use to work for me with Storage before. This only started happening yesterday. Really at a loss here... Could it be that somehow something got corrupted which causes Storage to fail on me?

Any help or advice on this matter would be greatly appreciated.

0 likes
1 reply
michaeldzjap's avatar
michaeldzjap
OP
Best Answer
Level 1

Got it! I was passing in a

$request->file('file);

to my save function instead of the actual file content. Wrapping the UploadedFile instance in a File::get did the trick. So:

Storage::disk('local')->put($path, File::get($request->file('file')));

solved in the end.

Please or to participate in this conversation.