gpt4 book ai didi

php - 如何在 Laravel 中从文件夹和数据库中删除文件或图像?

转载 作者:行者123 更新时间:2023-11-29 06:56:05 24 4
gpt4 key购买 nike

我已经创建了一个注册表。我创建了一个表来显示数据库中的所有数据。还有一个名为“删除”的按钮,我已经传递了唯一的 id。当我单击“删除”按钮时,它会从数据库中删除数据。但是,我想从数据库和文件夹中删除文件或图像。我的文件和图像存储在公共(public)文件夹中。

我该如何解决这个问题?

Delete button code. ( RegView.blade.php )

@foreach($data as $value )
<tr>
<td> <a href="delete{{ $value->id }}"><input type="submit" name="delete" value="Delete" class="btn-danger"></a> </td>
</tr>
@endforeach

Code for controller. ( RegViewController.blade.php )

 function delete($id)
{
DB::table('academic')->where('id',$id)->delete();
return redirect('RegView');
}

Here is the Routes that I have created.

Route::get('delete{id}','RegViewController@delete');

最佳答案

我可以提出一个解决方案,即:设置查询生成器来查找文件:

$query = DB::table('academic')->where('id',$id);

假设 file_path 是一个在表中保存路径的字段

$files_to_delete = $query->pluck('file_path')->toArray(); //keeping the result in a php 
$query->delete(); //now deleting
Storage::delete($files_to_delete);

PS: You can read more about similar methods with Storage in Laravel docs and only one important info in the doc is the following:

enter image description here

这意味着它会查找“storage/app”目录来查找该文件。

关于php - 如何在 Laravel 中从文件夹和数据库中删除文件或图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551170/

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