gpt4 book ai didi

php - 使用 Laravel 5.5 存储检查文件是否存在?头像存储

转载 作者:行者123 更新时间:2023-12-04 02:00:42 29 4
gpt4 key购买 nike

基于此代码: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',
],

还是什么都没有,我得到了一个 false for exists。

最佳答案

对于 future 的读者:

                   //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',
],

关于php - 使用 Laravel 5.5 存储检查文件是否存在?头像存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501209/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com