【问题标题】:cant store the file using storeAs in laravel无法在 laravel 中使用 storeAs 存储文件
【发布时间】:2021-02-09 16:09:02
【问题描述】:

iam 使用 laravel 站点,在我的文件中,我在路径中有一个目录 /var/www/site/admin.site.com/public/storage/salescall 我需要上传一个文件到这个 salescall 文件夹,我使用了以下代码。

     $filenameWithExt = $request->file('sales_call')->getClientOriginalName();
     $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
     $extension = $request->file('sales_call')->getClientOriginalExtension();
     $filenameToStore = $filename . '-' . time() . '.' . $extension;

     $request->file('sales_call')
    ->storeAs('', $filenameToStore, 'sales');

我的filesystems.php如下

'磁盘' => [

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

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],
    'sales' => [
        'driver' => 'local',
        'root' => public_path('salescall'),
        'visibility' => 'public',
    ],
],

文件没有上传到文件夹。请帮我解决这个问题。提前谢谢你。

【问题讨论】:

    标签: laravel file-upload


    【解决方案1】:

    您可以轻松做到这一点:

    $request->file('sales_call')-> storeAs('salescall/',$filenameToStore);
    

    为了更好地理解,请参见下文

    您的文件系统配置似乎不正确。 使用下面的示例作为指南,希望对您有所帮助。

    如果您使用此代码,它会将您的文件上传到 storage/app 文件夹中

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

    现在,如果您需要更改目录并直接存储到 storage 文件夹中,则需要更改 filesystems.php 文件中的内容。

    转到 config/filesystems.php 文件并更改以下代码-

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

    这里,这行代码'root' => storage_path('app'),负责定义存放位置。您只需根据自己的需求进行调整即可。

    或者,在您的控制器中,您可以执行以下操作:

    <?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;
        }
    }
    

    但在你自己的情况下,我相信:

    1. 在您的配置中,将根键设置为 storage_path('salescall')
    2. 你的应该是 $path = $request->file('sales_call')->storeAs('public/salescall',$fileNameToStore);

    请告诉我这是否可行。干杯!

    【讨论】:

    • 我已将 'local' => 更改为 'root' => storage_path('salescall'),并使用了 $request->file('sales_call')->storeAs('public/salescall' ',$fileNameToStore);但没有任何内容上传到文件夹
    • 请求正文中的文件名是sales_call吗?
    • 请同时对必要的文件进行截图。
    • 是的,文件文件名为 sales_call。我也附上了截图
    【解决方案2】:

    你必须这样使用

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
        'permissions' => [
            'file' => [
                'public' => 0664,
                'private' => 0600,
            ],
            'dir' => [
                'public' => 0775,
                'private' => 0700,
            ],
        ],
    ],
    
    $request->file('sales_call')-> storeAs('salescall/',$filenameToStore);
    

    【讨论】:

    • 我已将 'local' => 更改为 'root' => storage_path('salescall'),并使用了 $request->file('sales_call')-> storeAs('salescall/' ,$filenameToStore);但没有任何内容上传到文件夹
    • @user2830078 我更新了我的答案。请再次检查
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    相关资源
    最近更新 更多