Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Symfony2: Delete a temp file after response

I have a controller that generates an image and returns the image in the response.

use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

...

/**
 * @Rest\Get("/image/{name}")
 */
public function getImage($name) {
    $imageService = $this->get('image.service');
    $tempImage = $imageService->genImage($name);
    return new BinaryFileResponse($tempImage);
}

This works great but temp image never gets deleted.

How do I delete the temp image after the response is sent?

like image 913
Eric Avatar asked Sep 18 '25 03:09

Eric


1 Answers

I read though the implementation of BinaryFileResponse. Turns out there is a public method deleteFileAfterSend

I just need

return (new BinaryFileResponse($tempImage))->deleteFileAfterSend(true);
like image 198
Eric Avatar answered Sep 20 '25 19:09

Eric