jeudi 11 octobre 2018

Laravel Issue with Local File Upload

I have a rather simple html form, with the obligatory csrf field, as well as a file field, with the name "file" and type "file".

In my backend I have this function that is giving me grief and I'm not quite sure why...

public function store(Request $request){

    $validator = Validator::make($request->all() ,[
        'file' => 'required',
    ]);

       if ($validator->fails()) {
            return redirect()->back()->withInput()->withErrors($validator);
       }else{
        $file = $request->file('file');
        $name = $file->hashName();
        $type = $file->getClientMimeType();

        $path = $file->storeAs('/public/carrier/management/documents/', $name);

            if($type == "image/jpeg" || $type == "image/jpg" || $type == "image/png"){
                $img = Image::make(Storage::get($path));
                $img->sharpen(9);
                $img->resize(null, 480, function ($constraint) {
                    $constraint->aspectRatio();
                });
                $img->crop(320,480);
                $img->save(public_path('temp\images' . $name));

                Storage::put('/public/carrier/management/documents/thumbnails/'. $name, $img->__toString());
                $img->destroy();
            }

        $document = new carrierDocument();
        $document->title = $request->title;
        $document->type = $request->type;
        $document->date = $request->date;
        $document->date_expire = $request->date_expire;
        $document->notes = $request->notes;
        $document->documentID = $request->documentID;
        $document->filename = $filename;

        $document->save();

        return redirect()->back();
    }
}

I've tried testing this out but it hangs on the name = $file->hashName(); line with the error: Call to a member function hashName() on null.

I'm near certain I've set up the Validator correctly, so I assume the Validator is seeing that the file field has contents in it. This is part of my post data: file "Certificate.pdf"

Does anyone have any suggestions?

Before I forget, this is what I've included in my controller:

use Illuminate\Http\Request;
use Validator;
use Illuminate\Http\File;
use Storage;

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire