将图像上传到数据库时,在Laravel 6.0中出现错误

时间:2019-09-11 09:07:03

标签: php laravel image

我是Laravel的新手,正在使用Laravel 6.0。上传图片时出现 SplFileInfo :: getSize()错误:C:\ xamp \ tmp \ php14F3.tmp的统计信息失败

我在Google上搜索了解决方案,但找不到任何解决方案。

这是我的控制器功能

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

    }

2 个答案:

答案 0 :(得分:2)

我的解决方法是

使用Illuminate\Http\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.');

    }
}

工作正常。

答案 1 :(得分: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');
        }