Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FrazeColder's avatar

Spatie Media Library upload ALWAYS leads to “request does not have a file in a key named […]”

I have installed Spatie Media Library exactly how it is described on their docs. However, every time I try to save a media file with addMediaFromRequest('keyName') I get this error:

The current request does not have a file in a key named keyName

Every installation and getting started guide, such as this one on Laravel-News.com, is telling me to put this in my controller:

if (isset($data['avatar'])) {
    $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
}

That's also what I have done. Nevertheless I still get this "... request does not have a file ..." error... which really annoys me. I follow exactly the instalttion guides but it is not working.

This is how my Models, Controllers and View looks like for the registration procedure:

User.php:

use Glorand\Model\Settings\Traits\HasSettingsField;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Models\Role;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\Models\Media;

class User extends Authenticatable implements HasMedia
{
    use Notifiable;
    use HasRoles;
    use HasSettingsField;
    use HasMediaTrait;
    
    // ...
}

RegisterController.php (Note my // comments):

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    //If I check, if the avatar is set, I create a user without avatar
    if (isset($data['avatar'])) {
        $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
    }

    //If I don't check it, it leads to "... request does not have a file ..." error
    $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');

    return $user;
}

register.blade.php:

// ...
<div class="form-group row">
    <label for="avatar" class="col-md-4 col-form-label text-md-right">{{ __('Avatar (optional)') }}</label>

    <div class="col-md-6">
       <input id="avatar" type="file" class="form-control" name="avatar">
    </div>
</div>
// ...

The same also goes for other controllers where I want to give the possibility to upload avatars. For example my UserController leads to the same problem.

UserController.php

// ...
public function update(Request $request, User $user)
{
    if (isset($request->avatar)) {
        $user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
    }
}
// ...

However, if I dump the $request->avatar it is not empty! That actually MUST mean, that $request->avataris not empty BUT addMediaFromRequest('avatar') simply cannot access this variable or is trying to access another variable instead of the $request->avatar variable. The result I get when I dump $request->avatar:

Illuminate\Http\UploadedFile {#473 ▼
  -test: false
  -originalName: "23133.png"
  -mimeType: "application/octet-stream"
  -error: 1
  #hashName: null
  path: ""
  filename: ""
  basename: ""
  pathname: ""
  extension: ""
  realPath: "/Users/myname/Desktop/Coding/MyProject/public"
  aTime: 1970-01-01 00:00:00
  mTime: 1970-01-01 00:00:00
  cTime: 1970-01-01 00:00:00
  inode: false
  size: false
  perms: 00
  owner: false
  group: false
  type: false
  writable: false
  readable: false
  executable: false
  file: false
  dir: false
  link: false
}
0 likes
4 replies
DenMette's avatar

@frazecolder does your media collection exists?

Because by default you don't need to mention ->toMediaCollection('avatars'); this can just be ->toMediaCollection();

This information can be found on: https://docs.spatie.be/laravel-medialibrary/v7/basic-usage/associating-files/

It's a little bit tricky at first, but it just the way it works. Media collections are used when you want to have multiple association on one model, if you don't really need it, just skip it and work with the default.

    public function registerMediaCollections():void
    {
        $this->addMediaCollection('main')->singleFile();
        $this->addMediaCollection('my_multi_collection');
    }

I have for a model 2 media collections, one for the default with a single file, and the other one is just for testing some bits at the moment, but with multiple files.

Good luck

FrazeColder's avatar

It could be that this is my error. How can I check of my avatars media collection exists and furthermore how can I create one?

FrazeColder's avatar
FrazeColder
OP
Best Answer
Level 1

I finally found my error. My system is running on a Mac with Catalina. The problem is that I was hosting my project locally with php artisan serve. For some reason there where a problem with the permission to access the temp folder.

I am now running on valet without any hassle or error... maybe it's gonna help someone!

adiapr's avatar

You must add attribute enctype="multipart/form-data" in your form in your blade file

Please or to participate in this conversation.