Reputation: 363
Good day!
I think I read almost all the questions related to PHP and BOM, still I did not find a suitable answer to my problem. So here I am:
I have a PHP script (loader.php), the first time it is run it generates a configuration file (_config.php) In this script I just store some variables concerning the environement of the first call. If the _config.php file already exists I require it in loader.php
Everything works fine but the problem is that _config.php needs to be created as UTF8. The only way it worked for me, wrt this question, was with
file_put_contents(
$folder,
"\xEF\xBB\xBF".$phpCommands
);
Of course this adds the BOM and I read it when I use the require function the second time loader.php is called, generating in the end the well known extra space issue at the beginning of the page. I tried to remove it from the final output of the page using the method suggested here but it doesn't affect the result, probably because the BOM is inserted via require and not via fopen or similar.
All my PHP scripts are UTF-8 (without BOM). The generated _config.php is UTF-8 "with BOM".
To solve the problem I have two solutions but I can't figure out how to make them work:
Can someone help me out?
Please, don't suggest me alternative strategies to generate/store the configuration. It has to be done this way.
Thanks a lot!
Upvotes: 0
Views: 1499
Reputation: 522081
Simply do not add the BOM when creating the file. It serves no purpose as such.
The most likely explanation for your "the only way it worked for me" is simply a bad testing method, no more, no less. Meaning, your file was being created with UTF-8 perfectly fine, just whatever method you used to confirm that was flawed. I'll guess here that you opened the resulting file in some text editor, and that editor told you the file encoding was "ANSI" or "ASCII" or such.
Well, a plain text file does not declare its encoding anywhere. Your text editor was just taking a guess as to its encoding. If the file contents are just plain English/ASCII, there's no difference whatsoever between ASCII, ANSI and UTF-8. Your text editor simply told you one of the possible answers, where any answer is equally valid. Adding a BOM explicitly places a hint at the beginning of the file that the encoding is UTF-8, which the editor picked up on.
This, or something similar, is likely your entire non-issue.
Upvotes: 1