-1

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

babak
  • 55
  • 1
  • 7
  • 1
    you've a parse error, given if that's your actual code. – Funk Forty Niner Jan 30 '18 at 22:53
  • What exactly is different? Files do not carry encoding, every file is just a sequence of bytes. Open your files in a hex editor and check what bytes and corrupted in what way. – zerkms Jan 30 '18 at 22:58
  • If I open the damaged file in notepad++ and change the encoding to ANSI and save it then I can view it without any problems! – babak Jan 30 '18 at 23:16
  • 1
    Not sure how your comment helps you in any way: to find why a file is broken it's helpful to understand how exactly it's broken. To understand that you need to compare the exact bytes of one and another files. Notepad++ is not a suitable tool for the job (unless it has a proper hex viewer/editor there). – zerkms Jan 31 '18 at 00:55
  • I compared the damaged file with working file in HxD hex viewer and I see EF BB BF is added to the beginning of the returned TIFF file which can not be opened. I wonder how file sizes are the same!!. how can I stop php from adding BOM to the beginning of the file? – babak Feb 03 '18 at 04:52

1 Answers1

0

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

babak
  • 55
  • 1
  • 7