使用 Laravel 5.5 存储检查文件是否存在?阿凡达的存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47501209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if file exists with Laravel 5.5 Storage? Storage for Avatar
提问by Philipp Mochine
Based on this code: https://devdojo.com/episode/laravel-user-imageI 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:
基于此代码: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 );
}
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
所以我用 dd(Storage::disk('public')->exists('index.php')); 对其进行了测试。等我尝试了每个文件。我还在 filesystem.php 中添加了一个磁盘
'images' => [
'driver' => 'local',
'root' => storage_path('app/public/images'),
'visibility' => 'public',
],
Still nothing, I'm getting a false for exists.
仍然什么都没有,我得到了一个假的存在。
回答by Philipp Mochine
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:
使用 filesystem.php:
'images' => [
'driver' => 'local',
'root' => storage_path('app/public/images'),
'visibility' => 'public',
],
回答by lagbox
public_path()and the disk 'public' don't have the same root.
public_path()并且磁盘“公共”没有相同的根。
The public disk is probably pointing to something like: .../yoursite/storage/app/public
公共磁盘可能指向以下内容: .../yoursite/storage/app/public
public_path()would return something like: .../yoursite/public
public_path()会返回类似的东西: .../yoursite/public
The public disk is linked to the public folder at .../yoursite/public/storage-> .../yoursite/storage/app/public
公共磁盘链接到公共文件夹.../yoursite/public/storage->.../yoursite/storage/app/public

