如何将文件存储在自定义位置

How to store files in custom location

我需要在我的 Laravel 电子商务应用程序中上传文件并将其存储在相应用户的文件夹中。我的代码如下所示:

if ($request->hasFile('attach')) {
            try {
                $file = $request->file('attach');
                $filename = $file->getClientOriginalName();

                Storage::disk('local')->put($filename,  File::get($file));
                Storage::move('app/'.$filename, 'app/public/uploads/'.$user_id.'/attach/'.$filename);

我在 config/filesystems.php:

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

但是上传文件时出现错误:

(1/1) FileNotFoundException: File not found at path: app/Test1.docx

如何 store/save 自定义位置(例如用户文件夹)中的文件?存储上传文件的自定义位置如下所示: 'app/public/uploads/'.$user_id.'/attach/'

根据@sohel0415 的建议,这是对我有用的更新代码:

if ($request->hasFile('attach')) {
                    try {
                        $file = $request->file('attach');
                        $filename = $file->getClientOriginalName();
                        Storage::disk('local')->put('public/uploads/'.$user_id.'/attach/'.$filename, File::get($file));