Eric
Eric

Reputation: 7877

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?

Upvotes: 4

Views: 2751

Answers (1)

Eric
Eric

Reputation: 7877

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

I just need

return (new BinaryFileResponse($tempImage))->deleteFileAfterSend(true);

Upvotes: 16

Related Questions