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

发布于 2025-01-12 01:24:51 字数 1609 浏览 0 评论 0原文

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

在此处输入图像描述

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

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

,但没有一个有效。或者还有其他正确的方法可以做到这一点?

I try to delete the old profilepicture in storage file but it is not working.
below is my screenshot of my files

enter image description here

and this is my code in update profile controller.

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

i already tried

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

but none of them is working. Or there is any other correct way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2025-01-19 01:24:51

使用类 Filesystem 而不是 Storage,并给它一个绝对路径,如下所示:

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

您将成功删除文件!

Use class Filesystem instead of Storage, and give it an absolute path like this:

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

You will delete the file successful!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文