Skip to content

Commit 809dd9a

Browse files
committed
Making sure the tmp file is removed.
If an exception arise, the tmp file might not be removed. By using a finally block, we make sure we can remove this file, whatever happens.
1 parent 178018e commit 809dd9a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

docs/docs/09_Files/2_Temporary Files.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ use function Safe\unlink;
1616

1717
protected function createResponseWithXLSXAttachment(string $filename, Xlsx $xlsx): Response
1818
{
19-
$tmpFilename = Uuid::uuid4()->toString() . '.xlsx';
20-
$xlsx->save($tmpFilename);
21-
$fileContent = file_get_contents($tmpFilename); // Get the file content.
22-
unlink($tmpFilename); // Delete the file.
23-
19+
try {
20+
$tmpFilename = Uuid::uuid4()->toString() . '.xlsx';
21+
$xlsx->save($tmpFilename);
22+
$fileContent = file_get_contents($tmpFilename); // Get the file content.
23+
} finally {
24+
if (file_exists($tmpFilename)) {
25+
unlink($tmpFilename); // Delete the file.
26+
}
27+
}
28+
2429
return $this->createResponseWithAttachment(
2530
$filename,
2631
$fileContent
2732
);
2833
}
29-
```
34+
```

0 commit comments

Comments
 (0)