xtrontross
xtrontross

Reputation: 191

Laravel 5.2 upload image to storage

I want to ask, how can I make that somebody selects an image, then he clicks upload and the image will save into my project (example: project/public/images). Thank you :)

Upvotes: 1

Views: 3633

Answers (2)

Siddharth
Siddharth

Reputation: 1769

Or else you can do like this

    $destinationPath = '';
    $filename        = '';

    if (Input::hasFile('file')) {
        $file            = Input::file('file');
        $destinationPath = public_path().'/images/';
        $filename        = time() . '_' . $file->getClientOriginalName();
        $filename = str_replace(' ','_',$filename);
        $uploadSuccess   = $file->move($destinationPath, $filename);
    }

Upvotes: 0

scrubmx
scrubmx

Reputation: 2556

You can just do:

$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");

Storage::put($path, File::get($file->getRealPath()));

However. I would recommend to use laravel medialibrary package:

https://docs.spatie.be/laravel-medialibrary/v4/introduction

This way you can just do:

$post->addMediaFromRequest('image')->toCollection('images'); 

Upvotes: 2

Related Questions