0

I am using PHP and IIS 8 on a Windows Server 2012. I create an UTF-8 encoded file on the server and then I push it to the client with the following code:

                // Start upload to client
                $fullPath = $full_name;
                $fsize = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                $ext = strtolower($path_parts["extension"]);
                header("Content-type: application/octet-stream");
                header("Content-Disposition: attachment; filename=\"".$file_name."\"");
                header("Content-length: $fsize");
                header("Cache-control: private");
                readfile($full_name);

When the client receives the file it contains a starting BOM and (oh surprise) 3 characters are missing at the end of the file. I have checked the file on the server and it is saved there correctly (no BOM saved and the three missing characters are there).

The PHP script which creates and uploads the file has the header

header('Content-Type:text/html; charset=UTF-8');

I have resorted to add 3 times "line feed" at the end of the file to get the three missing characters. I could also have added +3 to the variable $fsize, but I do not feel comfortable doing that kind of cheat (it might shoot back). I think there should be a more elegant way out of this.

Curiously I am using the same code on a Win7 machine with IIS 7.5 and there is no issue there with the UTF-8 BOM addition. The PHP directory is a copy of the directory on the Win7 machine, including the php.ini file.

Can someonen see what am I missing?

Thanks in advance for your help.

4

1 回答 1

0

您还应该检查脚本文件中的 BOM。通常如果您的 IDE 使用 BOM 保存 UTF-8 文件,它位于开始<?php标记之前,因此 php 将其视为输出。

于 2013-05-13T14:15:32.973 回答