Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hamzajamshed152's avatar

How to upload image to public/assets in laravel?

I am working with PHP Laravel 8. I am doing avatar upload to the database and uploading it to public/assets. It's working with database but it doesn't upload it I want some help Thank You

This is HTML code for uploading

    <div class="form-group">
     <label for="exampleInputFile">Profile Image</label>
       <div class="input-group">
          <div class="custom-file">
       <input type="file" name="profile_image" class="custom-file-input" id="profile-image">
            <label class="custom-file-label" for="exampleInputFile">Recommended Profile Image (160w x 160h)</label>
          </div>
       </div>
              <span class="font-italic text-warning">Picture size should be less than 4MB</span>
     </div>

and here is the controller code

public function update(Request $request)
{
    $user = Auth::user();    
   // $user->full_name=$request->input('full_name');
    //$user->email=$request->input('email');
    $user->avatar=$request->input('profile_image');
    if($request->hasFile('profile_image'))
    {
        $avatar = $request->file('profile_image');

        $filename = time().'.'.$avatar->getClientOriginalExtension();
        $location = url('public/assets/uploads/images/'.$filename);
        Image::make($avatar)->resize(300, 300)->save($location);
        $user = Auth::user();
        $user->avatar = $filename;
    }
    auth()->user()->save();
    $user->save(); 
    return redirect()->back()->with('success','Sucessfully changed User Information');
}
0 likes
32 replies
tykus's avatar

You save a file to a path, not a URL:

//$location = url('public/assets/uploads/images/'.$filename);
$location = public_path('assets/uploads/images/'.$filename);
hamzajamshed152's avatar

@tykus it's not uploading the image ... can you tell me if I need any image package for this ? I searched it and nothing worked in this issue I don't know why it happened

lat4732's avatar

@hamzajamshed152 According to your code you are using Image Intervention, aren't you? You can install it through

composer require intervention/image

then use it at the top of your controller

use Image;

and it has to work.

hamzajamshed152's avatar

@Laralex i used it but it couldn't help whole controller code is here @tykus can you check either

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use DB;
use App\Models\User;
use Illuminate\Support\Facades\Auth; 
use Image;
class ProfileController extends Controller
{
    public function update(Request $request)
    {
        $user = Auth::user();
        $user->full_name=$request->input('full_name');
        $user->email=$request->input('email');
        $user->avatar=$request->input('profile_image');
        if($request->hasFile('profile_image'))
        {
            $avatar = $request->file('profile_image');
            $filename = time().'.'.$avatar->getClientOriginalExtension();
            $location = public_path('assets/uploads/images/'.$filename);
            Image::make($avatar)->resize(300, 300)->save($location);
            $user = Auth::user();
            $user->avatar = $filename;
        }
        auth()->user()->save();
        $user->save(); 
        return redirect()->back()->with('success','Sucessfully changed User Information');
    }
}
tykus's avatar
tykus
Best Answer
Level 104

@hamzajamshed152

    public function update(Request $request)
    {
        $user = auth()->user();
        $user->full_name=$request->input('full_name');
        $user->email=$request->input('email');
        if($request->hasFile('profile_image'))
        {
            $avatar = $request->file('profile_image');
            $filename = time().'.'.$avatar->getClientOriginalExtension();
            $location = public_path('assets/uploads/images/'.$filename);
            Image::make($avatar)->resize(300, 300)->save($location);
            $user->avatar = $location;
        }
        $user->save(); 
        return redirect()->back()->with('success','Sucessfully changed User Information');
    }
1 like
hamzajamshed152's avatar

@tykus Thanks you correct many things but why it does not store the picture in that folder. public/assets/uploads/images ..nothing comes up

tykus's avatar

@hamzajamshed152 do you have a public/assets/uploads/images/ directory in your project; and is it writable by the web server?

hamzajamshed152's avatar

@tykus one more thing now this code is not updating the database ... i don't know why this if condition is not getting to it

tykus's avatar

@hamzajamshed152 what is happening; are you getting an Exception message? Is there anything that might point towards the failure?

Are you actually uploading the file at all; does the form have the multipart/form-data attribute?

hamzajamshed152's avatar

@tykus yeah it has multipart/form-data ... and I not getting any Exception its saving and return success but not uploading it to folder and when I remove this line before If condition which you removed

$user->avatar=$request->input('profile_image');

its not even save to database also

tykus's avatar

@hamzajamshed152 when the input is a file type; then $request->input('profile_image') will be null - so how was this working previously? Do you have another (text) input on the form with name profile_image? `

Snapey's avatar

You will NEVER need this

$user->avatar=$request->input('profile_image');
Snapey's avatar

Maybe your file input is called something else. Perhaps show the form?

tykus's avatar

@Snapey we are told that this is the form input;

<input type="file" name="profile_image" class="custom-file-input" id="profile-image">

But, based on the OP's other replies; something else is happening here... possibly there is another input with the same name 🤷‍♂️

hamzajamshed152's avatar

@Snapey @tykus

  <form role="form" action="{{ route('profile.update') }}" method="POST" nctype="multipart/form-data">
                         @csrf
                        <div class="card-body">
                            <div class="form-group">
                                <label for="full_name">Full Name</label>
                                <input type="text" value="{{ $data->full_name }}" name="full_name" class="form-control" id="full_name" placeholder="Enter Your Full Name">
                            </div>

                            <div class="form-group">
                                <label for="exampleInputEmail1">Email address</label>
                                <input type="email" value="{{ $data->email }}" name="email" class="form-control" id="user_email" placeholder="Enter Your Email">
                            </div>

                            <div class="form-group">
                                <label for="exampleInputFile">Profile Image</label>
                                <div class="input-group">
                                    <div class="custom-file">
                                        <input type="file" name="profile_image" class="custom-file-input" id="profile-image">
                                        <label class="custom-file-label" for="exampleInputFile">Recommended Profile Image (160w x 160h)</label> 
                                    </div>
                                </div>
                                <span class="font-italic text-warning">Picture size should be less than 4MB</span>
                            </div>
                        </div>
                        <div class="card-footer">
                        <button type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </form>
Snapey's avatar
nctype="multipart/form-data">

should be enctype

1 like
hamzajamshed152's avatar

@Snapey Thank you .. i think i just type it in a hurry but one more error comes up tykus change this code and this code come up with ErrorException Undefined variable:$image can you check this for me please

public function update(Request $request)
{
    $user = auth()->user();
    $user->full_name=$request->input('full_name');
    $user->email=$request->input('email');
    $user->avatar=$request->input('profile_image');
    if ($request->hasFile('profile_image')) {
        $avatar = $request->file('profile_image');
        $filename = now()->timestamp . '.' . $avatar->getClientOriginalExtension();
        $image->move(public_path('assets/uploads/images/'), $filename);
        Image::make($avatar)->resize(300, 300)->save($image);
        $user->avatar = $location;
    }
    $user->save(); 
    return redirect()->back()->with('success','Sucessfully changed User Information');
}
Snapey's avatar

@hamzajamshed152

I type it in a hurry

Then spend several days on laracasts looking for a solution.

There is a lesson there

hamzajamshed152's avatar

@Snapey I checked this code previously but it's not updating in the database so I add one line I removed it but it makes ErrorExpection of Undefined variable $image also

Please or to participate in this conversation.