0

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');

    }
8

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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');
        }

Comments

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.