1

I was working on a project and was trying to develop a file uploading system for skins. When I tried to upload my skin, I was given "Call to a member function storeAs() on null"

    public function uploadSkin(Request $request)
    {
        /* $request->validate([
            'skins' => 'required|mimes:png|max:1024',
        ]); */

        $storage_dir = storage_path('app/skins');

        $request->file('skins')->storeAs($storage_dir, Auth::user->name . '.png');

        return route('settings')->with('success', 'skin uploaded :)');

    }

Form code:

    <form method="post" enctype="multipart/form-data" action="/settings">
        @csrf
        <br/>
        <div class="form-group">
            <input type="file" class="form-control-file" id="skins" name="skins" required>
        </div>
        <button type="submit" class="btn btn-success">Upload</button>
    </form>
4
  • Is your file input named "skins"? Can you maybe also post the form/relevant fields in your question? Commented Apr 21, 2020 at 6:08
  • Please paste your form codes Commented Apr 21, 2020 at 6:09
  • Ok, added them. Commented Apr 21, 2020 at 6:16
  • What have you tried to debug the problem? Commented Apr 21, 2020 at 6:34

2 Answers 2

1

To store a file like an image or any kind of files you can use a code like this:

public function uploadSkin(Request $request){
    $image = $request->file('skins');
    if ($image != null) {
            $image->move('uploads/skins/', Auth::user()->name . $image->getClientOriginalExtension());
        }
    return route('settings')->with('success', 'skin uploaded :)');
}
Sign up to request clarification or add additional context in comments.

Comments

0

To uppload a file there are various ways in the laravel but for now you can try this to simply move your file to your directory:

if($files= $request->file('skins')){  
   $files->move('uploads/skins/', Auth::user->name . '.png');
}

2 Comments

What is "this"? Please add some explanation to your answer such that others can learn from it
And why should that be tried? According to the question itself, $request->file('skins') is NULL on uploading a file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.