Skip to content

Commit 38d9e04

Browse files
author
Julien Neuhart
authored
Merge pull request #21 from moufmouf/tmp_file_finally
Making sure the tmp file is removed.
2 parents adda031 + 809dd9a commit 38d9e04

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)