Yêu cầu thg 6 23, 2021 3:54 SA 178 0 2
  • 178 0 2
0

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

Chia sẻ
  • 178 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 CÂU TRẢ LỜI


Đã trả lời thg 6 23, 2021 3:57 SA
Đã được chấp nhận
+3

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

return response()->download($zipFile)->deleteFileAfterSend();
Chia sẻ
Đã trả lời thg 6 23, 2021 4:02 SA
+1

bạn dùng

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

nó sẽ xoá file zip sau khi response

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí