Adams_'s avatar

Image saving in database as C:\wamp64\tmp\phpAE1C.tmp instead of saving in public/images

I am trying to edit/update an image in a user table, when I select a new image and submit the form the image name doesn't get stored in the database instead this C:\wamp64\tmp\phpAE1C.tmp get saved in the image column, I don't know why, Please help if u know why.

The UsersController

           public function update(Request $request, $id)
      {
            $request->validate([
            'name' => 'required|string|max:225',
            'email' => 'required|string|email|max:255|unique:users,email,'.auth()->id(),
             'password' => 'sometimes|nullable|string|min:6|confirmed',
           ]);

       $user = auth()->user();

         //Handle avatar Upload
          if ($request->hasFile('avatar')) {
        // Get filename with extention 
        $filenamewithExt = $request->file('avatar')->getClientOriginalName();
        // Get just filename
        $filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
        // Get just Extention
        $extention = $request->file('avatar')->getClientOriginalExtension();
        // Filename to store
        $filenameToStore = $filename.'_'.time().'.'.$extention;
        // Upload Image
        $path = $request->file('avatar')->storeAs('public/avatars', $filenameToStore);

    }

    if ($request->hasFile('avatar')) {
        
        $user->avatar = $filenameToStore;
  
       
    }

    //Handle image Upload

    if ($request->hasFile('image')) {
        // Get filename with extention 
        $ImageNameWithExt = $request->file('image')->getClientOriginalName();
        // Get just filename
        $ImageName = pathinfo($ImageNameWithExt, PATHINFO_FILENAME);
        // Get just Extention
        $Extentions = $request->file('image')->getClientOriginalExtension();
        // Filename to store
        $ImageNameToStore = $ImageName.'_'.time().'.'.$Extentions;
        // Upload Image
        $paths = $request->file('image')->storeAs('public/images', $ImageNameToStore);

    }

    if ($request->hasFile('image')) {
        
        $user->image = $ImageNameToStore;

        
    }
    
     $user->save();

    $input = $request->except('password', 'password_confirmation');

     if (!$request->filled('password')) {
           $user->fill($input)->save();

    return back()->with('success', 'Profile updated successfully!');
     }

      $user->password = bcrypt($request->password);
      $user->fill($input)->save();

        return back()->with('success', 'Profile and password updated successfully');
    }






      The image input field in edit blade file

       <div class="form-group col-md">
    
      <div class="custom-file">
      <input type="file" name="image" class="custom-file-input" id="customFile" >
      <label class="custom-file-label text-align-left" style="text-align:left;" 
      for="customFile">Choose file</label>
       </div>

       </div>
0 likes
3 replies
jlrdw's avatar

Looks like you are naming it after the if brackets, name it inside the if brackets.

Sorry braces not brackets.

1 like
Adams_'s avatar
Adams_
OP
Best Answer
Level 2

Make these changes to the code and it's working now! Thank you!

     $input = $request->except('password', 'password_confirmation');


      to this,


      $input = $request->except('password', 'password_confirmation','image','avatar');

      if ($request->hasFile('image')) {
           $input['image'] = $ImageNameToStore;
   }

      if ($request->hasFile('avatar')) {
           $input['avatar'] = $filenameToStore;
   }
jlrdw's avatar

Glad you got it figured it out, sorry if my answer didn't help.

1 like

Please or to participate in this conversation.