Skip to content
Advertisement

Upload image in Laravel

I try these code in controller:

public function uploadimage(Request $request)
    {
        if ($request->hasFile('image')) {
            $destinationPath = 'uploads';
            $files = $request->image;
            $file_name = $files->getClientOriginalName();
            $files->move($destinationPath, $file_name);
            echo "Complete";
        } else {
            echo "No File";
        }
    }

My Html input file:

 <input name="image" type="file" id="image"/>

but i can’t upload. Any idea?

Advertisement

Answer

you can use storage::putFile('folder_name', $request->image) this will return automatic generated filename and you can store that in your database.

And make sure your have your default file system set to public and your form has this enctype="multipart/form-data"

Reference https://laravel.com/docs/5.5/filesystem#storing-files

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement