导致下载文件损坏的原因是什么?

I am currently stugling to understand just what is wrong with my app.

As I was unable to find a solution (more about that here: Codeigniter Download Helper corrupts .7z and .rar files help or alternative solutions?) I decided to take a different approach.

So the question is what causes downloaded files via the HTML request (if I said that right) to become corrupt even when they are not corrupted when downloaded via FTP. And a follow up is, what causes a specific type of extension to become corrupt. Since I can download zip files fine but 7z and rar files get corrupted.

This is the header list that codeiniter's download helper uses:

        header('Content-Type: "'.$mime.'"');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: ".strlen($data));

The helper uses file_get_contents() in order to determine content-length and send. I tried to use filesize() and to send a file via readfile() but the result was the same.

I also tried different mimes, from the all inclusive application/octet-stream to the spefic ones depending on the type (application/x-7z-compressed etc) but it had little effect.

So at this point I have no idea what causes some extensions to work while others do not.

Any suggestion is welcome.

Is there some php, or apache or any tweak, property I can use to change this?

EDIT:

I noticed that for some reason when I open a file there is a added number that represents an ID of the download i have no idea how it got here or how to stop it, but this have put me on the right track. Thanks so very much, i will update this one as I progress.

Also how to prevent php to append something on top of my file?

EDIT:

FOUND IT!

It was an obscure echo that I simply didn't see, I was just so tired. But now that it is gone everything works fine. I've spend around hours truing to find out what was wrong, I am pissed of at myself, but right now I am glat it is over. Thanks so very much helping me find the problem :)