Shadow Shoot
Shadow Shoot

Reputation: 27

Getting error in Laravel 6.0 while upload image to database

I'm new to Laravel and using Laravel 6.0. While uploading image I am getting error of SplFileInfo::getSize(): stat failed for C:\xamp\tmp\php14F3.tmp

I searched for solution on google yet couldn't find any solution.

This is my controller function

public function store(PostsCreateRequest $request)
    {
        //
        $input = $request->all();

        $user = Auth::user(); 

        if ($request->hasfile('photo_id')) {

            $file = $request->file('photo_id');


            $name = time() .$size. $file->getClientOriginalName();

            $file->move('posts' , $name);

            $photo = Photo::create(['path'=>$name]);

            $input['photo_id'] = $photo->id;

        }

       $user->posts()->create($input);

        Session::flash('created_post',"The Post  has been created");
        return redirect('/home');

    }

Upvotes: 0

Views: 944

Answers (2)

Ankit Ror
Ankit Ror

Reputation: 21

My solution is

use Illuminate\Http\Request; this request instead of old request.

    public function saveimage(Request $request){

      request()->validate([
         'file'  => 'required|mimes:jpeg,jpg|max:2048',
       ]);

       if ($files = $request->file('file')) {
           $destinationPath = 'public/images/'; // upload path
           $profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();
           $files->move($destinationPath, $profilefile);
           $insert['file'] = "$profilefile";
        }

        $check = Document::insertGetId($insert);

        return Redirect::to("home")
        ->withSuccess('Great! file has been successfully uploaded.');

    }
}

Its working fine.

Upvotes: 2

Piyali
Piyali

Reputation: 182

Give the size of the image while giving the validation. The below code is working fine.


public function saveImage(Request $request){
            $this->validate($request, [
           'name' => 'required',
           'description' => 'required',
           'image' => 'required|image|mimes:jpeg,jpg,gif,png,svg|max:2048'
           ]);

            $instrument = new \App\Models\Instrument;
            $instrument->name = $request->input('name');
            $instrument->description = $request->input('description');
            $imgfile = $request->file('image');
            $instrument->image = $imgfile->getClientOriginalName();

            if ($imgfile !== null) {
              $filenameWithExt = $imgfile->getClientOriginalName();
              $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
              $extension = $imgfile->getClientOriginalExtension();
              $fileNameToStore= $filename.'_'.time().'.'.$extension;
              $imgfile->storeAs('public/images', $fileNameToStore);

          } else {
            //dd("Image Not Uploaded");
          }

          $instrument->save();
          return redirect('/instruments')->with('success', 'Details are uploaded successfully');
        }

Upvotes: 0

Related Questions