Cannot save my uploaded image to local disk folder

你离开我真会死。 提交于 2020-04-17 20:29:30

问题


I am currently building a CRUD application using Laravel. It requires me to upload images and information but seems like there are some problems on storing the images to the localdisk folder.

Here is my controller code:

 public function store(Request $request)
    {
        $lostitem =new Admin();


        $this->validate($request, [
            'date' => 'required',
            'TimeFound' => 'required',
            'AreaWhereFound' => 'required',
            'image' => 'required',
            'Remark' => 'required',
            'DateClaimed' => 'required',
            'TimeClaimed' => 'required',
            'CategoryID'=>'required'
        ]);


        $uuid = Str::uuid()->toString();



        // $record = new Admin;
        // return view('students.create');
        $lostitem->code = $uuid;
        $lostitem->date = $request->date;

        $lostitem->TimeFound = $request->TimeFound;
        $lostitem->AreaWhereFound = $request->AreaWhereFound;
        $lostitem->image = $request->image;


         if($request->hasfile('image'))
       {
            $filenameWithExt=$request->file('image')->getClientOriginalName();
            $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
            $extension =$request->file('image')->getClientOriginalExtension();
            $fileNameToStore=$filename.'_'  .time().'.'.$extension;
            $path=$request->file('image')->storeAs('public/images',$fileNameToStore);


                // $file = $request->file('image');
                //  $extension =$file->getClientOriginalExtension();//getting image extensionimage
                //  $filename=time()  ."." .$extension;

                //   $file->move('uploads',$filename->getClientOriginal);

                //   //getting from data base


            }
         else
         {
            //  $lostitem->image = "";
            $fileNameToStore='noimage.jpg';

         }
        $lostitem->image = $request->image ;
        $lostitem->Remark = $request->Remark;
        $lostitem->DateClaimed = $request->inputDateClaimed;
        $lostitem->TimeClaimed = $request->TimeClaimed;
        $lostitem->CategoryID = $request->CategoryID;

        $lostitem->save();
         return redirect(route('LostItem_add'))->with('successMsg', 'Record added!');
    }

The other information is saved. I hope to get help.


回答1:


Change Your controller code to this:

public function store(Request $request)
{
    $lostitem =new Admin();
    $this->validate($request, [
        'date' => 'required',
        'TimeFound' => 'required',
        'AreaWhereFound' => 'required',
        'image' => 'required',
        'Remark' => 'required',
        'DateClaimed' => 'required',
        'TimeClaimed' => 'required',
        'CategoryID'=>'required'
    ]);

    $uuid = Str::uuid()->toString();

    $lostitem->code = $uuid;
    $lostitem->date = $request->date;
    $lostitem->TimeFound = $request->TimeFound;
    $lostitem->AreaWhereFound = $request->AreaWhereFound;
    $lostitem->image = $request->image;

    if($request->hasfile('image')){
        $filenameWithExt=$request->file('image')->getClientOriginalName();
        $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
        $extension =$request->file('image')->getClientOriginalExtension();
        $fileNameToStore=$filename.'_'  .time().'.'.$extension;
        $path=$request->file('image')->move(public_path('images/'),$fileNameToStore);
    }
    else{
        $fileNameToStore='noimage.jpg';
    }

    $lostitem->image = $request->image ;
    $lostitem->Remark = $request->Remark;
    $lostitem->DateClaimed = $request->inputDateClaimed;
    $lostitem->TimeClaimed = $request->TimeClaimed;
    $lostitem->CategoryID = $request->CategoryID;
    $lostitem->save();
    return redirect(route('LostItem_add'))->with('successMsg', 'Record added!');
}

And then access your image in the blade like this

<img src="{{ asset('images/'.$item->image) }}">

And make sure that you do have an folder named "images" in the public directory




回答2:


Save the path of image in database

public function store(Request $request)
{
    $lostitem =new Admin();
    $this->validate($request, [
        'date' => 'required',
        'TimeFound' => 'required',
        'AreaWhereFound' => 'required',
        'image' => 'required',
        'Remark' => 'required',
        'DateClaimed' => 'required',
        'TimeClaimed' => 'required',
        'CategoryID'=>'required'
    ]);

    $uuid = Str::uuid()->toString();

    $lostitem->code = $uuid;
    $lostitem->date = $request->date;
    $lostitem->TimeFound = $request->TimeFound;
    $lostitem->AreaWhereFound = $request->AreaWhereFound;
    $lostitem->image = $request->image;

    if($request->hasfile('image')){
        $filenameWithExt=$request->file('image')->getClientOriginalName();
        $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
        $extension =$request->file('image')->getClientOriginalExtension();
        $fileNameToStore=$filename.'_'  .time().'.'.$extension;
        $path=$request->file('image')->move(public_path('images/'),$fileNameToStore);
    }
    else{
        $fileNameToStore='noimage.jpg';
    }

    $lostitem->image = $path ;
    $lostitem->Remark = $request->Remark;
    $lostitem->DateClaimed = $request->inputDateClaimed;
    $lostitem->TimeClaimed = $request->TimeClaimed;
    $lostitem->CategoryID = $request->CategoryID;
    $lostitem->save();
    return redirect(route('LostItem_add'))->with('successMsg', 'Record added!');
}

& show it

<img src="{{ asset('images/'.$item->image) }}">


来源:https://stackoverflow.com/questions/61232006/cannot-save-my-uploaded-image-to-local-disk-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!