【问题标题】:Check if file exists with Laravel 5.5 Storage? Storage for Avatar使用 Laravel 5.5 存储检查文件是否存在? Avatar 的存储空间
【发布时间】:2018-05-10 02:49:34
【问题描述】:

基于此代码:https://devdojo.com/episode/laravel-user-image 我创建了以下代码以上传头像并删除旧头像。我尝试使用 Storage:Facade 但我不确定它是否正确。那么让我们看一下我的代码摘录:

                    use Illuminate\Support\Facades\Storage;
                    ..

                    $avatar = $request->file('avatar');

                    $filename = time() . '.' . $avatar->getClientOriginalExtension();      

                    //Using Image intervention, storing to Public/Images/user
                    Image::make($avatar)->orientate()->fit(220)->save( public_path('/images/user/' . $filename ) );
                    $user = Auth::user();

                    $oldavatar = $user->avatar;

                    $user->avatar = $filename;
                    $user->save();

                    //Delete old avatar
                    if($oldavatar != 'profile.jpg' and Storage::disk('public')->exists('/images/user/' . $oldavatar );){

                        Storage::disk('public')->delete('/images/user/' . $oldavatar );
                    }

所以我用 dd(Storage::disk('public')->exists('index.php')); 测试了它等等我尝试了所有的文件。我还在filesystem.php中添加了一个磁盘

    'images' => [
        'driver' => 'local',
        'root' => storage_path('app/public/images'),
        'visibility' => 'public',
    ],

仍然没有,我得到了一个错误的存在。

【问题讨论】:

    标签: php laravel storage laravel-5.5 file-exists


    【解决方案1】:

    对于未来的读者:

                       //e.g. user/hashMD5.jpg
                        $filename = $avatar->hashName('user');
    
                        $image = Image::make($avatar)->orientate()->fit(220);
    
                        $location = Storage::disk('images')->put($filename, (string) $image->encode());
    
                        if($location){
                            $user = Auth::user();
    
                            $oldfilename = $user->avatar;                           
    
                            $oldfileexists = Storage::disk('images')->exists( $oldfilename );
    
                            //Delete old avatar
                            if($oldfilename != 'user/profile.jpg' and $oldfileexists){
                                Storage::disk('images')->delete( $oldfilename );
                            }  
    
                            //Save current image to database. Sollte ich nicht update benutzen?
                            $user->avatar = $filename;
                            $user->update();
                        }
    

    使用filesystem.php:

            'images' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images'),
            'visibility' => 'public',
        ],
    

    【讨论】:

    • 感谢您回来并给出正确答案!您的解决方案有效@PhilippMochine
    【解决方案2】:

    public_path() 和磁盘 'public' 的根目录不同。

    公共磁盘可能指向类似:.../yoursite/storage/app/public

    public_path() 会返回类似:.../yoursite/public

    公共磁盘链接​​到公共文件夹.../yoursite/public/storage -> .../yoursite/storage/app/public

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2016-11-12
      • 2017-11-02
      • 1970-01-01
      相关资源
      最近更新 更多