Laravel saving image to public folder?

36,932

Solution 1

Try with move() method?

$filename = time().'.'.request()->img->getClientOriginalExtension();
request()->img->move(public_path('images'), $filename);

$user->image=$filename;
$user->save();

Solution 2

you need to add name="img"on your form.

Share:
36,932
Przemek
Author by

Przemek

Updated on June 25, 2021

Comments

  • Przemek
    Przemek almost 3 years

    So I am trying to save uploaded image to laravel/public folder and return src so a path to where the file has been saved, however nothing happens after I choose an image and I get no errors, what might be wrong?

    public function testing(Request $request) {
        if(Input::file())
        {
            $image = Input::file('img');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $path = public_path('images/' . $filename);
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
            $user->image = $filename;
            $user->save();
        }
    }
    
    
    <form action="{{ action('BuilderController@testing') }}" enctype="multipart/form-data" role="form" method="POST">
        <input id="img" class="form-control filestyle margin images" data-input="false" type="file" data-buttonText="Upload Logo" data-size="sm" data-badge="false" onchange="uploadImage();" />
    </form>
    
  • Przemek
    Przemek about 7 years
    I get: Creating default object from empty value however image is being saved why?