anikkhan's avatar

Call to a member function getClientOriginalExtension() on a non-object

Hey I try to upload file but im getting this error "Call to a member function getClientOriginalExtension() on a non-object"

public function add(){
        $file = Request::file('file');
        $extension = $file->getClientOriginalExtension();
        Storage::disk('local')->put($file->getFilename().'.'.$extension, File::get($file));

        $entry = new \App\File();
        $entry->mime = $file->getClientMimeType();
        $entry->original_filename = $file->getClientOriginalName();
        $entry->filename = $file->getFilename().'.'.$extension;
        $entry->save();

        $product = new Product();
        $product->file_id=$entry->id;
        $product->name=Request::input('name');
        $product->description=Request::input('description');
        $product->price=Request::input('price');
        $product->imageurl=Request::input('imageurl');
        $product->save();

        return redirect('/products');
    }

0 likes
1 reply
bashy's avatar

This works fine for me. Make sure your form has the right fields in and it allows files to be send (enctype="multipart/form-data") or with the ['files' => true] in the form helper

public function add(Request $request) {
    $file = $request->file('file');

    return $file->getClientOriginalExtension();
}

Please or to participate in this conversation.