iT邦幫忙

0

請教 Laravel 刪除多個file 寫法

klz 2021-07-20 13:49:36468 瀏覽

我是在MYSQL中找取檔名, 以下是可正常運行,
但可否Storage::delete中指示刪除多個檔案,
請教一些比較簡潔寫法.

        $deletePic123 = DB::table('db')->where('ID',$id)->get();
        $PIC_PATH1 = $deletePic123[0]->PIC_PATH1;
        $PIC_PATH2 = $deletePic123[0]->PIC_PATH2;
        $PIC_PATH3 = $deletePic123[0]->PIC_PATH3;
        Storage::delete('img/'.$PIC_PATH1);
        Storage::delete('img/'.$PIC_PATH2);
        Storage::delete('img/'.$PIC_PATH3);

謝謝各位大大

1 個回答

0
耿直小伙
iT邦新手 2 級 ‧ 2021-07-20 15:19:53
最佳解答

Laravel File Storage

use Illuminate\Support\Facades\Storage;

Storage::delete('file.jpg');

Storage::delete(['file.jpg', 'file2.jpg']);
// 僅供參考
$deletePics = DB::table('db')
    ->where('ID', $id)
    ->get();

$picCount = 3;
foreach ($deletePics as $deletePic) {
    for ($i = 1; $i <= $picCount; $i++) {
        $column = 'PIC_PATH' . $i;
        $path = $deletePic->{$column} ?? '';
        ($path !== '') && Storage::delete('img/' . $path);
    }
}
klz iT邦新手 5 級 ‧ 2021-07-23 17:25:46 檢舉

謝謝指教 很棒的教學

我要發表回答

立即登入回答