response->header("Pragma: public"); $this->response->header("Cache-Contr" /> response->header("Pragma: public"); $this->response->header("Cache-Contr" /> response->header("Pragma: public"); $this->response->header("Cache-Contr"/>

Cakephp 2, response body content type not working

693 Views Asked by At
$this->autoRender = false;
$this->response->header("HTTP/1.1 200 OK");
$this->response->header("Pragma: public");
$this->response->header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
$this->response->header("Cache-Control: private", false);

//$this->response->header("Content-type: image/png"); //tried also
$this->response->type('jpg');

$this->response->body(readfile($file));

return $this->response;

Always returns Content-Type: text/html; charset=UTF-8.

Thanks

2

There are 2 best solutions below

1
voskys On BEST ANSWER
$this->response->body(readfile($file));

Change this line to

$this->response->body(file_get_contents($file));

And works.

I know that it is more performant readfile, but at the moment, should work.

Probable something that readfile breaks with the enconding.

0
Drea58 On

You can download a file more easily with:

$this->autoRender = false;
$this->response->file($path, array('download' => true, 'name' => $name));