We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

struggling with $response->setHeader

Hi all,

in my app, I have to retrieve images that are located outside the public directory. Each image is also referenced in a db table (with mime-type, size, access rights and so on)

I want to dispaly them with {{ image("media/image/13" }}

media controller, image action below:

public function image($id)
// some stuff to build absolute path to image: $imagePath
// ....

// also tried with: 

        $response->setHeader("X-Sendfile", $imagePath);

No image is displayed. Content-Length is not the right one ... What am I doing wrong ? Any help really appreciated !

response headers with setFileToSend:

Connection:Keep-Alive Content-Description:File Transfer Content-Disposition:attachment; filename=22.jpg Content-Length:465 Content-Transfer-Encoding:binary Content-Type:image/jpeg

response headers with X-Sendfile:

Connection:Keep-Alive Content-Length:45 Content-Type:image/jpeg Date:Sun, 08 Dec 2013 20:34:06 GMT Keep-Alive:timeout=5, max=98 Server:Apache/2.2.22 (Debian) X-Powered-By:PHP/5.4.4-14+deb7u5 X-Sendfile:/var/www/phalcon/medias/app/files/2013-12-08/22.jpg

are you sure the $imagePath has the complete path to the image including the image name?

What are you get at? php var_dump(file_exists($imagePath));

edited Nov '16

Hi, thank you for help. $imagePath is OK, I'm now dealing this problem with

$response = new Phalcon\Http\Response();
        return $response;

It's not really satisying me, but I will use that as a primary workaround before I investigate (and learn more about response headers) this problem further ...


It looks like it is non-documented feature. send() method on response is not gonna be called at all. But this is the method which reads the file content. So try following (note send() call at the end):

    public function downloadAction()
        $this->response->setFileToSend('/path/to/my/file', 'file-name.png')->send();