Laravel - Does not save image with Storage ::


I'm trying to save an image using the Storage of Laravel

Storage::disk('avatars')->put($avatarName, $request->file('avatar'))
$avatarName = avatar'.time().'.'.$request->file('avatar')->getClientOriginalExtension();

The problem is that it does not save it as an image, only a text appears:


Someone knows why you are not saving the image correctly, the name and extension places it correctly, but apparently does not save the image as such.

asked by DoubleM 02.01.2019 в 22:03

1 answer


For the storage of files in laravel you can use the "store" method that belongs to the Request class, I show you the following example, with a normal load and one with the name of the file (commented code):


     class HelloController extends Controller { 

         public function store(Request $request)
            if($request->hasFile('avatar')){ //validamos si existe algún archivo en el "Request"
             #Almacenando la imagen, store indicando la ruta
             $path = $request->file('avatar')->store('public/avatar'); 

             #ya que si quieres renombrar utiliza esto
             //$path = $request->file('avatar')->storeAs(
                       //'public/avatar', "custom_name"
        } else {
        //To Do: Mensaje de advertencia


On the other hand I recommend that you declare the variable with the name of the image first and then use the variable in the "Storage" method I hope it helps, greetings!

answered by 02.01.2019 в 22:23