要从存储中删除的正确图像路径 laravel 8

Correct image path to delete from storage laravel 8

我尝试删除存储文件中的旧个人资料图片,但它不起作用。 下面是我的文件截图

这是我在更新配置文件控制器中的代码。

public function updateProfile(Request $request, User $user)
{
    if ($request->hasFile('profilepicture')) {

        Storage::delete('/storage/profilepicture' . $user->profilepicture);

        $filenameWithExt = $request->file('profilepicture')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        $extension = $request->file('profilepicture')->getClientOriginalExtension();
        $fileNameToStore =  time() . '.' . $extension;
        $path = $request->file('profilepicture')->storeAs('public/profilepicture', $fileNameToStore);

        $user->update([
            'profilepicture' => $fileNameToStore
        ]);
        
    }

    $user->update([
        'name' => $request->name,
        'email' => $request->email,
        'birth_date' => $request->birth_date,
        'section' => $request->section,
        'unit' => $request->unit,
        'phone' => $request->phone,
    ]);

    return back()->withSuccess('You have successfully update User.');
}

我已经试过了

Storage::delete('/public/storage/profilepicture/' . $user->profilepicture);
Storage::delete('/storage/profilepicture/' . $user->profilepicture);
Storage::delete('profilepicture/' . $user->profilepicture);

但其中 none 个正在运行。或者还有其他正确的方法吗?

使用 class 文件系统而不是存储,并为其指定一个绝对路径,如下所示:

// Declare    
use Illuminate\Filesystem\Filesystem;
    
// Using
$file = 'icon-256x256.png';
$path = public_path('profilepicture/' . $file);
Filesystem::delete($path);

您将删除文件成功!