[{"data":1,"prerenderedAt":312},["ShallowReactive",2],{"content-query-nmdcQydUbZ":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"category":5,"createdAt":10,"updatedAt":10,"body":11,"_type":306,"_id":307,"_source":308,"_file":309,"_stem":310,"_extension":311},"/article/coding/delete-file-after-call-export-file-api-in-laravel","coding",false,"","在 Laravel 中下載檔案 api 送出後刪除檔案","專案有兩隻 API 是去產生 zip 檔後，分別為寄送信件和回傳檔案給前端，寄送信件 API 有特別定義 Response 格式，因這兩隻產生檔案採共用 function 會產生檔案後放在 storage/app 內，但回傳檔案 API 本來我只想就回傳檔案就好不想真的存一份檔案在伺服器上，所以有套件是可以直接做一個串流回傳 zip 檔的，像是:","2024-07-04",{"type":12,"children":13,"toc":304},"root",[14,31,43,48,293,298],{"type":15,"tag":16,"props":17,"children":18},"element","p",{},[19,22,29],{"type":20,"value":21},"text","專案有兩隻 API 是去產生 zip 檔後，分別為寄送信件和回傳檔案給前端，寄送信件 API 有特別定義 Response 格式，因這兩隻產生檔案採共用 function 會產生檔案後放在 ",{"type":15,"tag":23,"props":24,"children":26},"code",{"className":25},[],[27],{"type":20,"value":28},"storage/app",{"type":20,"value":30}," 內，但回傳檔案 API 本來我只想就回傳檔案就好不想真的存一份檔案在伺服器上，所以有套件是可以直接做一個串流回傳 zip 檔的，像是:",{"type":15,"tag":16,"props":32,"children":33},{},[34],{"type":15,"tag":35,"props":36,"children":41},"a",{"href":37,"rel":38,"target":40},"https://github.com/maennchen/ZipStream-PHP",[39],"nofollow","_blank",[42],{"type":20,"value":37},{"type":15,"tag":16,"props":44,"children":45},{},[46],{"type":20,"value":47},"但因為寫法共用的關係(我就懶)會讓寄送信件 API Response 會變成串流資料，所以這套件暫時不適合我的專案，那只好將檔案存放在伺服器上了，接下來要解決的是如何在回傳檔案 API 後刪除檔案，用排程定時刪嗎? 好像不太好，可能會將同時間進來的 Request 做好的檔案刪掉，有沒有比較好的方式呢，在早期官方文件(新版文件沒有)有提到這樣的功能:",{"type":15,"tag":49,"props":50,"children":54},"pre",{"className":51,"code":52,"language":53,"meta":7,"style":7},"language-php shiki shiki-themes material-theme-palenight","public function download(ExportValidation $request)\n{\n    // 將檔案儲存在 storage/app ...\n    $response = response()->download(storage_path('app') . '/' . $zipName . '.zip');\n    return $response->deleteFileAfterSend(true);\n}\n","php",[55],{"type":15,"tag":23,"props":56,"children":57},{"__ignoreMap":7},[58,109,118,128,250,284],{"type":15,"tag":59,"props":60,"children":63},"span",{"class":61,"line":62},"line",1,[64,70,75,81,87,93,98,104],{"type":15,"tag":59,"props":65,"children":67},{"style":66},"--shiki-default:#C792EA",[68],{"type":20,"value":69},"public",{"type":15,"tag":59,"props":71,"children":72},{"style":66},[73],{"type":20,"value":74}," function",{"type":15,"tag":59,"props":76,"children":78},{"style":77},"--shiki-default:#82AAFF",[79],{"type":20,"value":80}," download",{"type":15,"tag":59,"props":82,"children":84},{"style":83},"--shiki-default:#89DDFF",[85],{"type":20,"value":86},"(",{"type":15,"tag":59,"props":88,"children":90},{"style":89},"--shiki-default:#FFCB6B",[91],{"type":20,"value":92},"ExportValidation",{"type":15,"tag":59,"props":94,"children":95},{"style":83},[96],{"type":20,"value":97}," $",{"type":15,"tag":59,"props":99,"children":101},{"style":100},"--shiki-default:#BABED8",[102],{"type":20,"value":103},"request",{"type":15,"tag":59,"props":105,"children":106},{"style":83},[107],{"type":20,"value":108},")\n",{"type":15,"tag":59,"props":110,"children":112},{"class":61,"line":111},2,[113],{"type":15,"tag":59,"props":114,"children":115},{"style":83},[116],{"type":20,"value":117},"{\n",{"type":15,"tag":59,"props":119,"children":121},{"class":61,"line":120},3,[122],{"type":15,"tag":59,"props":123,"children":125},{"style":124},"--shiki-default:#676E95;--shiki-default-font-style:italic",[126],{"type":20,"value":127},"    // 將檔案儲存在 storage/app ...\n",{"type":15,"tag":59,"props":129,"children":131},{"class":61,"line":130},4,[132,137,142,147,152,157,162,166,171,175,180,186,190,195,200,205,210,214,218,222,227,232,236,241,245],{"type":15,"tag":59,"props":133,"children":134},{"style":83},[135],{"type":20,"value":136},"    $",{"type":15,"tag":59,"props":138,"children":139},{"style":100},[140],{"type":20,"value":141},"response ",{"type":15,"tag":59,"props":143,"children":144},{"style":83},[145],{"type":20,"value":146},"=",{"type":15,"tag":59,"props":148,"children":149},{"style":77},[150],{"type":20,"value":151}," response",{"type":15,"tag":59,"props":153,"children":154},{"style":83},[155],{"type":20,"value":156},"()->",{"type":15,"tag":59,"props":158,"children":159},{"style":77},[160],{"type":20,"value":161},"download",{"type":15,"tag":59,"props":163,"children":164},{"style":83},[165],{"type":20,"value":86},{"type":15,"tag":59,"props":167,"children":168},{"style":77},[169],{"type":20,"value":170},"storage_path",{"type":15,"tag":59,"props":172,"children":173},{"style":83},[174],{"type":20,"value":86},{"type":15,"tag":59,"props":176,"children":177},{"style":83},[178],{"type":20,"value":179},"'",{"type":15,"tag":59,"props":181,"children":183},{"style":182},"--shiki-default:#C3E88D",[184],{"type":20,"value":185},"app",{"type":15,"tag":59,"props":187,"children":188},{"style":83},[189],{"type":20,"value":179},{"type":15,"tag":59,"props":191,"children":192},{"style":83},[193],{"type":20,"value":194},")",{"type":15,"tag":59,"props":196,"children":197},{"style":83},[198],{"type":20,"value":199}," .",{"type":15,"tag":59,"props":201,"children":202},{"style":83},[203],{"type":20,"value":204}," '",{"type":15,"tag":59,"props":206,"children":207},{"style":182},[208],{"type":20,"value":209},"/",{"type":15,"tag":59,"props":211,"children":212},{"style":83},[213],{"type":20,"value":179},{"type":15,"tag":59,"props":215,"children":216},{"style":83},[217],{"type":20,"value":199},{"type":15,"tag":59,"props":219,"children":220},{"style":83},[221],{"type":20,"value":97},{"type":15,"tag":59,"props":223,"children":224},{"style":100},[225],{"type":20,"value":226},"zipName ",{"type":15,"tag":59,"props":228,"children":229},{"style":83},[230],{"type":20,"value":231},".",{"type":15,"tag":59,"props":233,"children":234},{"style":83},[235],{"type":20,"value":204},{"type":15,"tag":59,"props":237,"children":238},{"style":182},[239],{"type":20,"value":240},".zip",{"type":15,"tag":59,"props":242,"children":243},{"style":83},[244],{"type":20,"value":179},{"type":15,"tag":59,"props":246,"children":247},{"style":83},[248],{"type":20,"value":249},");\n",{"type":15,"tag":59,"props":251,"children":253},{"class":61,"line":252},5,[254,260,264,269,274,279],{"type":15,"tag":59,"props":255,"children":257},{"style":256},"--shiki-default:#89DDFF;--shiki-default-font-style:italic",[258],{"type":20,"value":259},"    return",{"type":15,"tag":59,"props":261,"children":262},{"style":83},[263],{"type":20,"value":97},{"type":15,"tag":59,"props":265,"children":266},{"style":100},[267],{"type":20,"value":268},"response",{"type":15,"tag":59,"props":270,"children":271},{"style":83},[272],{"type":20,"value":273},"->",{"type":15,"tag":59,"props":275,"children":276},{"style":77},[277],{"type":20,"value":278},"deleteFileAfterSend",{"type":15,"tag":59,"props":280,"children":281},{"style":83},[282],{"type":20,"value":283},"(true);\n",{"type":15,"tag":59,"props":285,"children":287},{"class":61,"line":286},6,[288],{"type":15,"tag":59,"props":289,"children":290},{"style":83},[291],{"type":20,"value":292},"}\n",{"type":15,"tag":16,"props":294,"children":295},{},[296],{"type":20,"value":297},"這樣子可以直接回傳檔案後直接刪除，不需要排程另外刪囉！",{"type":15,"tag":299,"props":300,"children":301},"style",{},[302],{"type":20,"value":303},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":111,"depth":111,"links":305},[],"markdown","content:article:coding:delete-file-after-call-export-file-api-in-laravel.md","content","article/coding/delete-file-after-call-export-file-api-in-laravel.md","article/coding/delete-file-after-call-export-file-api-in-laravel","md",1766375576061]