2

I have some images in "app/var/assets" (I have to put these images in that directory, I can't change this by client restriction).

I need to show these images. App directory is not an accesible path for Apache, so I need to use X-Send File.

How can I use X-Send File for this?

I tried in my controller:

$path = $this->get('kernel')->getRootDir() . '/var/assets/example.jpg';

$response = new BinaryFileResponse($path');

$response->trustXSendfileTypeHeader();
$response->headers->set('Content-type', 'image/jpg');
$response->sendHeaders();
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, "name"); 

And then in my view:

<img src="<?= $response ?>" />

But the image is not found and the url I get for the image is:

HTTP/1.0 200 OKCache-Control: publicContent-Disposition: inline; filename=

Any idea?

user2794692
  • 361
  • 2
  • 10
  • 24

2 Answers2

0

I had a similar problem and I created an action that returns an image.

public function getImgAction(){
    $f = fopen('/var/assets/example.jpg', "rb");
    $str = stream_get_contents($f);
    fclose($f);
    $response = new Response($str, 200);
    $response->headers->set('Content-Type', 'image/jpg');
    return $response;
}
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
0

Make sure that mod_xsendfile is installed and enabled for apache.

There's a typo in line 3, string delimiter when passing variable to class constructor (should be $response = new BinaryFileResponse($path);, but I assume you're aware of that).

Erik Theoboldt
  • 2,168
  • 2
  • 16
  • 21