检查文件是否存在 Laravel 5.5 存储?头像存储
Check if file exists with Laravel 5.5 Storage? Storage for Avatar
基于此代码: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',
],
还是什么都没有,我得到了一个错误的存在。
public_path()
和磁盘 'public' 没有相同的根。
public 磁盘可能指向如下内容:.../yoursite/storage/app/public
public_path()
会 return 类似于:.../yoursite/public
public 磁盘链接到 public 文件夹,位于 .../yoursite/public/storage
-> .../yoursite/storage/app/public
对于未来的读者:
//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',
],
基于此代码: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',
],
还是什么都没有,我得到了一个错误的存在。
public_path()
和磁盘 'public' 没有相同的根。
public 磁盘可能指向如下内容:.../yoursite/storage/app/public
public_path()
会 return 类似于:.../yoursite/public
public 磁盘链接到 public 文件夹,位于 .../yoursite/public/storage
-> .../yoursite/storage/app/public
对于未来的读者:
//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',
],