我正在使用FilePond进行图像上传.它为上传的图像生成随机字符串,并以数组的形式保存在数据库中.

我try 执行以下操作,它会删除记录,但不会删除与其关联的文件.

Since it's an array, I tried this:

foreach($this->attachments as $attachment) {
            if(File::exists(storage_path($attachment))) {
                Storage::delete($attachment);
            }
        }

我还try 了这个:

if(Storage::exists($this->attachments))
{
  Storage::delete($this->attachments);
}

注: 我使用Filament作为管理仪表板.

这些文件将在storage/app/public/vehicle-images保存

我做了php artisan storage:link次,所以公共文件夹显示``Public/store/Vehicle-Images```

推荐答案

在本例中,文件位于:Laravel/storage/app/public/vehicle-images

$filename = 'test.txt';

if(\File::exists(storage_path('app/public/vehicle-images/'.$filename))){
   \File::delete(storage_path('app/public/vehicle-images/'.$filename));
}

为了更好地了解文件的位置,在此之后,您只需执行循环判断/删除即可.

你可以在这里阅读更多:https://laravel.com/docs/9.x/filesystem#the-public-disk

您还可以在以后为该文件夹指定一个磁盘,以使其更易于访问.

Finally:

foreach($this->attachments as $attachment) {

    //Option #1: if $attachment == file.txt
    //use this code:
    if(\File::exists(storage_path('app/public/vehicle-images/'.$attachment))){
       \File::delete(storage_path('app/public/vehicle-images/'.$attachment));
    }
    
    //Option #2: if $attachment == vehicle-images/file.txt
    //use this code:
    if(\File::exists(storage_path('app/public/'.$attachment))){
       \File::delete(storage_path('app/public/'.$attachment));
    }

}

100

Laravel相关问答推荐

在Laravel Lumen中迭代连接查询数据

如何在Laravel中从多行中获取合并数组?

在Laravel Livewire中,如果一个输入基于另一个唯一的字段是唯一的,如何判断验证?

Laravel 9 中使用 WHERE 子句的列的 SUM

如何在 Laravel 中使用 Vue 路由?

Laravel:BelongstoMany 关系实现

Laravel 5.8 在从已清除的会话中单击注销后显示419 Page Expired

如何在 Laravel 4 中使用没有 id 的 Eloquent 更新数据

Laravel Input:all() 忽略禁用的输入

在 laravel 分页中添加一些数据

WhereNotExists Laravel Eloquent

如何将Carbon 转换为字符串,只取日期?

如果用户未登录 Laravel,则重定向到登录

Laravel Eloquent 查询生成器默认 Where 条件

在 laravel Passport 客户端应用程序中使用访问令牌获取用户数据

如何在没有日志(log)、没有信息的情况下调试 Laravel 错误 500

如何使用队列设置高、低和中优先级Electron邮件?

Laravel:如何在 PhpUnit 上启用堆栈跟踪错误

如何从 laravel 5.1 中数据库表的 created_at 属性中 Select 年份和月份?

有没有办法让表名自动添加到 Eloquent 查询方法中?