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

vandan's avatar
Level 13

how to file upload in laravel directly into storage folder

how to uplaod file direct in storage folder file

1 like
4 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

If you use this code, it will upload your file in storage/app folder-

Storage::disk('local')->put('file.txt', 'Contents');

Ref: https://laravel.com/docs/5.8/filesystem

Now, if you need to change the directory and store into the storage folder directly, you need to change something in the filesystems.php file.

Go to config/filesystems.php file and change the following code-

'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

Here, this line of code 'root' => storage_path('app'), responsible to define where to store. You just adjust according to your demands.

4 likes
Victor Paredes's avatar

Just stopped by to let you know that this was super helpful. Thank you.

1 like
patrickadvance's avatar

<?php

namespace App\Http\Controllers\Api;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class UserAvatarController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        
        $request->validate([
            'avatar'=> ['required','max:200'] 
        ]);

        $user = User::findOrFail(auth()->user()->id);

        // Handle file Upload
        if($request->hasFile('avatar')){

            //Storage::delete('/public/avatars/'.$user->avatar);

            // Get filename with the extension
            $filenameWithExt = $request->file('avatar')->getClientOriginalName();
            //Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('avatar')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('avatar')->storeAs('public/avatars',$fileNameToStore);

       $user->avatar = $fileNameToStore ;

            $user->save(); 
        }

       
       
        
        return $user;
    }
}
3 likes

Please or to participate in this conversation.