Search code examples
phpkohana-3.2

Trying to send a tiff file in php to client but the file is damaged after download


I'm trying to send a tiff file from a controller in Kohana framework version 3.2 (I know it's a bit old) using the response->send_file() method, file is downloaded in browser and the size is ok. but when I try to view it I get an error showing the file is damaged. I download the same file using ssh and I can view it without any problems. when I compare the files in notepad++ encoding for the working file is ANSI but for the damaged one is utf-8-bom. this is the code for the method in my controller:

public function action_file() {
    $this->auto_render = false;
    $path = '/tmp/test.tiff'
    $this->response->send_file($path);
}

I read the Kohana send_file source code and I see it is using:

echo fread(...)

to send the file to client. How can I change the encoding on output buffer so the file be in ANSI (Windows-1252) format? I tried

mb_http_output('Windows-1252');
mb_internal_encoding('Windows-1252');

with no suuccess


Solution

  • I added ob_clean() before sending file and problem is solved. It seems somewhere in my codes or the framework BOM was added to output buffer and the file was corrupted

    public function action_file() {
        $this->auto_render = false;
        $path = '/tmp/test.tiff';
        ob_clean();
        $this->response->send_file($path);
    }
    

    thanks zerkms for mentioning comparing files in a hex editor