Skip to content

Commit

Permalink
Merge pull request #21 from moufmouf/tmp_file_finally
Browse files Browse the repository at this point in the history
Making sure the tmp file is removed.
  • Loading branch information
gulien authored Dec 18, 2020
2 parents adda031 + 809dd9a commit 38d9e04
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions docs/docs/09_Files/2_Temporary Files.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ use function Safe\unlink;

protected function createResponseWithXLSXAttachment(string $filename, Xlsx $xlsx): Response
{
$tmpFilename = Uuid::uuid4()->toString() . '.xlsx';
$xlsx->save($tmpFilename);
$fileContent = file_get_contents($tmpFilename); // Get the file content.
unlink($tmpFilename); // Delete the file.

try {
$tmpFilename = Uuid::uuid4()->toString() . '.xlsx';
$xlsx->save($tmpFilename);
$fileContent = file_get_contents($tmpFilename); // Get the file content.
} finally {
if (file_exists($tmpFilename)) {
unlink($tmpFilename); // Delete the file.
}
}

return $this->createResponseWithAttachment(
$filename,
$fileContent
);
}
```
```

0 comments on commit 38d9e04

Please sign in to comment.