Asked Jun 23rd, 2021 3:54 a.m. 200 0 2
  • 200 0 2
0

Cách xóa file zip sau khi download trong Laravel

Share
  • 200 0 2

Mình đang dùng ZipArchive để nén và download xuống trong laravel, nhưng gặp 1 vấn đề là sau khi download thì tất cả file zip đc giữ lại folder public/
Có cách nào để nó ko lưu file zip lại đây không ạ.
Mình cảm ơn

$zip = new ZipArchive();
$zipFile = 'csv_sets_' . time() . '.zip';
$zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString(self::buildFileNameClass($createTime), $csvContent);
$zip->close();
return response()->download($zipFile);

2 ANSWERS


Answered Jun 23rd, 2021 3:57 a.m.
Accepted
+3

Bạn dùng deleteFileAfterSend() nhé.

return response()->download($zipFile)->deleteFileAfterSend();
Share
Answered Jun 23rd, 2021 4:02 a.m.
+1

bạn dùng

return response()->download($zipFile)->deleteFileAfterSend();

nó sẽ xoá file zip sau khi response

Share
Viblo
Let's register a Viblo Account to get more interesting posts.