nizam0786's avatar

Uploaded files are not be stored correctly? only 1kb?

Hi guys,

I have been trying to upload a file and store it in the local storage area of a Laravel project I am working on.

The file is being uploaded however, it cannot be opened because it is not being stored correctly. The file size is only showing 1kb I have tried different files and also file types such as PDF and word documents but still not luck.


        Storage::disk('local')->put($driverFile->path, $driverFile);

it returns success so I am assuming it just needs more time to upload the file? any ideas?

thanks in advance.

0 likes
5 replies
click's avatar
click
Best Answer
Level 35

See https://laravel.com/docs/5.6/filesystem#file-uploads and see the signature of the method you are using.

->put($path, $contents, $options);
->putFile($path, $file, $options);

I do not know the contents of your variable $driverFile but if it is a file object you should use putFile instead of put().

1 like
nizam0786's avatar

@m-rk I tried using putFile instead but it returned BadMethodCallException.

Call to undefined method Illuminate\Database\Query\Builder::hashName()


namespace App\Models;

use Illuminate\Support\Facades\Storage; use Symfony\Component\HttpFoundation\File\UploadedFile;

class AddFileToDriver {

protected $driver;
protected $file;
protected $type;

 public function __construct(Driver $driver, UploadedFile $file, $type) {
    $this->driver = $driver;
    $this->file = $file;
    $this->type = $type;
}


public function save() {
    $driverFile = $this->driver->addFile($this->makeFile());

    Storage::disk('local')->putFile($driverFile->path, $driverFile);
}


protected function makeFile() {
    return new DriverFile(
        [
            'document_type' => $this->type,
            'name' => $this->makeFileName(),
            'mime' => $this->file->getClientMimeType(),
            'original_filename' => $this->file->getClientOriginalName(),
        ]);
}


protected function makeFileName() {
    
    $name = sha1( time() . $this->file->getClientOriginalName());

    $extension = $this->file->getClientOriginalExtension();

    return "{$name}.{$extension}";
}

}

click's avatar

that error is not related to storing the file. The error must be somewhere else and I can't see where from here. Just look at the exception tree and figure out where it goes wrong.

Please or to participate in this conversation.