user9266939
user9266939

Reputation:

Laravel Upload Image to img folder

Whats the best way for me to upload an image or more with form class to my Storage?

Blade:

<form class="" action="{{route('newproduct')}}" method="post">
<div class="form-group">
        <div class="form-group">
          <label for="myimage">  <img src="/img/{{$user->myimage}}" height="80" width="80"></label>
          <input type="file" id="myimage" name="myimage"></font>

        </div>

      </div>

Controller:

   public function NewProduct(Request $request){
      $user = Auth::user();
      $user->myimage= date('mdYHis') . uniqid() . $request->myimage;
      $user->save();
      return redirect()->back();
    }

Here in my Controller it saves only the random generated name in the DB, but dont upload the file to my Server. I want use this path for saving path:

/public/img/

Updated Code without image path:

    public function NewProduct (Request $request)
      if($request->hasFile('myimage')){
    if ($request->file('myimage')->isValid()) {
      $user = Auth::user();
      $image_name = date('mdYHis') . uniqid() . $request->file('myimage')->getClientOriginalName();
      $user->save();
      return redirect()->back();
    }

}

Thanks

Upvotes: 2

Views: 5339

Answers (2)

First, add enctype="multipart/form-data" to the <form>

Then, in your controller add this:

public function NewProduct(Request $request){
    if($request->hasFile('myimage')){
        if ($request->file('myimage')->isValid()) {
            $user = Auth::user();
            $image_name = date('mdYHis') . uniqid() . $request->file('myimage')->getClientOriginalName();
            $path = base_path() . '/public/img';
            $request->file('myimage')->move($path,$image_name);
            $user->myimage = $path.$image_name;
            $user->save();
            return redirect()->back();
        }
        return redirect()->back()->with('error', 'image not valid');
    }
    return redirect()->back()->with('error', 'no image');
}

For multiple images just need to iterate over the myimage array:

public function NewProduct(Request $request){
    if($request->hasFile('myimage')){
        // Do a loop over all images
        foreach ($request->file('myimage') as $image) {
            if ($image->isValid()) {
                $user = Auth::user();
                $image_name = date('mdYHis') . uniqid() . $image->getClientOriginalName();
                $path = base_path() . '/public/img';
                $image->move($path,$image_name);
                $user->myimage = $path.$image_name;
                $user->save();
                return redirect()->back();
            }
        }
        // end loop
    }
    return redirect()->back()->with('error', 'no image');
}

One problem with this approach is that the user will take the last image to store it (All images will be uploaded but the user image path will be the last one).

It can be solved creating a table to store user images paths and saving the image there (one to many relationship)

Upvotes: 1

Saurabh Mistry
Saurabh Mistry

Reputation: 13689

Blade :

<form  action="{{route('newproduct')}}" method="post" enctype="multipart/form-data">
     <div class="form-group">
        <div class="form-group">
          <label for="myimage">  
          <input type="file" id="myimage" name="myimage"></font>
        </div>
      </div>
</form>

Controller :

use Illuminate\Support\Facades\Storage;

  public function NewProduct(Request $request){

        // if request has file
        if($request->hasFile('myimage')){

            $filenameWithExt=$request->file('myimage')->getClientOriginalName();

            $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);

            $extension=$request->file('myimage')->getClientOriginalExtension();

            $fileNameToStore= date('mdYHis') . uniqid() .$filename.'.'.$extension;

            request()->myimage->move(public_path('img'), $fileNameToStore);  

     }else{
           // if no file from request , then put one no-image.jpeg in public/img folder
           $fileNameToStore='no-image.jpeg'
      }

  $user = Auth::user();
  $user->myimage=$fileNameToStore;
  $user->save();
  return redirect()->back();
  }

Upvotes: 0

Related Questions