dougang2749
2017-11-26 21:29
浏览 458
已采纳

检查Laravel 5.5 Storage是否存在文件? 阿凡达的存储

Based on this code: https://devdojo.com/episode/laravel-user-image I created the following code in order to upload an avatar and delete the old one. I'm tried to use Storage:Facade but I'm not sure if it is the right way. So let's look at my code excerpt:

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

So I tested it out with dd(Storage::disk('public')->exists('index.php')); etc. I tried every file there is. I also added a disk in the filesystem.php with

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

Still nothing, I'm getting a false for exists.

图片转代码服务由CSDN问答提供 功能建议

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

 使用Illuminate \ Support \ Facades \ Storage; 
 .. 
 
 $ avatar = $ request-> file('  avatar'); 
 
 $ filename = time()。  ''  。  $ avatar-> getClientOriginalExtension();  
 
 //使用图像干预,存储到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(); 
 
 //删除旧头像
 if($ oldavatar!='profile.jpg'和存储::磁盘('公共') - >存在('/  images / user /'。$ oldavatar);){
 
存储::磁盘('公共') - >删除('/ images / user /'。$ oldavatar); 
} 
   
 
 

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

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

仍然没有,我对存在感到错误。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtui0650 2017-11-27 15:37
    已采纳

    For future readers:

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

    With the filesystem.php:

            'images' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images'),
            'visibility' => 'public',
        ],
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douren7921 2017-11-27 05:55

    public_path() and the disk 'public' don't have the same root.

    The public disk is probably pointing to something like: .../yoursite/storage/app/public

    public_path() would return something like: .../yoursite/public

    The public disk is linked to the public folder at .../yoursite/public/storage -> .../yoursite/storage/app/public

    评论
    解决 无用
    打赏 举报