代码之家  ›  专栏  ›  技术社区  ›  Philipp Mochine

检查文件是否存在Laravel 5.5存储?头像存储

  •  3
  • Philipp Mochine  · 技术社区  · 7 年前

    基于此代码: 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')->存在('index.php');等等。我试了所有的文件。我还在文件系统中添加了一个磁盘。php与

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

    还是没什么,我得到了一个虚假的存在。

    2 回复  |  直到 7 年前
        1
  •  4
  •   Philipp Mochine    7 年前

    对于未来的读者:

                       //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();
                        }
    

    使用文件系统。php:

            'images' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images'),
            'visibility' => 'public',
        ],
    
        2
  •  0
  •   lagbox    7 年前

    public_path() 并且磁盘“public”没有相同的根。

    公共磁盘可能指向以下内容: .../yoursite/storage/app/public

    公共路径() 将返回以下内容: .../yoursite/public

    公用磁盘链接到位于的公用文件夹 .../yoursite/public/storage -&燃气轮机; .../yoursite/storage/app/public