Character problem in file creating

I have a creatingFile.php which has the code below.

[b]creatingFile.php[/b]

<?php 
file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', 'englishText'); 
?> 

After I browse creatingFile.php, I find createdFile.php.
Yes, It’s successfully created.
As I browse createdFile.php, I can see the text “englishText”.
So far, so good.

BUT, as I change creatingFile.php like the below. a tiny problem which I can’t solve happens.

[b]creatingFile.php[/b]

<?php 
file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', '[COLOR="#FF0000"]koreanText[/COLOR]'); 
?> 

As I browse createdFile.php, I can see some broken characters instead of the text “koreanText”.

I go to the directory which has the files “creatingFile.php” and “createdFile.php”.
As I open the file “createdFile.php” with the old friend notePad selecting UTF-8, I can see the text “koreanText”.
After I save “createdFile.php” with the notePad, I can see the text “koreanText” in the browsing result.

I like to see the text “koreanText” in the browsing result without openning and saving the file "createdFile.php with the notePad.

How can I see the text “koreanText” when I directly browse the file “createdFile.php” before working with the notePad?

It definitely sounds like a character encoding issue. You could try using utf8_encode() on the Korean text, eg:

<?php
file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', utf8_encode('koreanText'));
?>

If that doesn’t work I’m sure someone who knows more about character encoding than me will be able to help :slight_smile:

> [COLOR=#464646]As I browse createdFile.php, I can see some broken characters instead of the text “koreanText”.

what do you mean exactly by "[/COLOR]I browse createdFile.php"? how do you go about doing that?

utf8_encode('koreanText')

It doesn’t work, but worth to try, thank you marbean, anyway.

browse means to see the file with a browser(explorer, chrome, or fireFox …).

for example, if you browse the file below, you will see “1111”.

[b]file.php[/b]

$myVar=1111;
echo $myVar;

try

[LEFT][COLOR=#000000][FONT=Consolas]header[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]'Content-Type: text/html; charset=utf-8'[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]);

at the start of your file[/FONT][/COLOR][/LEFT]

As I browse the file creatingFile.php, it says “Warning: Cannot modify header information - headers already sent by (output started at creating.php:1) in creating.php on line 2”.

How can I fix the code below?

[b]creatingFile.php[/b]
<?php  
header('Content-Type: text/html; charset=utf-8');
file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', utf8_encode('koreanText'));  
?>

headers need to execute before any (including even a single literal white space character for example) characters are output.

I don’t quite understand the code you’ve supplied exactly, but taking it literally, the way to modify it would be this:


<?php
header('Content-Type: text/html; charset=utf-8');
?>creatingFile.php
<?php  
file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', utf8_encode('koreanText'));  
?>

I don’t quite understand what “creatingFile.php” is representing. Anyway, make sure the header command is placed in the line of execution of your code before any output takes place.

I think I understand what you mean because I sometimes see the WARNING when I use “head(“location: $goingTo_someWhere”);”.

Oh, no. “creatingFile.php” is just a name of the file.

The code below is in the file “creatingFile.php”.
And the code below is all code in the file “creatingFile.php”.

<?php
header('Content-Type: text/html; charset=utf-8');
file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', utf8_encode('koreanText'));
?>

As you browse it at the temporal URL http://dot.kr/x-test/creatingFile.php, you can see the warning.
There is no space before “<?php” and after “?>”.

I made the code below in the file “creating2.php” for testing “header(‘Content-Type: text/html; charset=utf-8’);”.

[b]creating2.php[/b]

<php? header('Content-Type: text/html; charset=utf-8'); ?>

The file “creatingFile2.php” has the code above, and the code above is all code in the file "creatingFile2.php, and there is no space before “<?php” and after “?>”.

You can see it at http://dot.kr/x-test/creatingFile2.php, but I can still see the warning when I browse it.
What is the problem in the code of “creatingFile2.php”?

well as I said, you need the php where the header() is, to not have anything come before it. not a single return. so the first character in the file needs to be <, that’s the < of the <?php bit. and that includes any other files being included, linked to. make the header() the first thing which happens. and make no characters at all preceded the <?php bit.

I think I keep the all the rule you said.

The first character is “<”.
There is no include.

As I said, the below is the all code in creatingFile.php. and you can see the result at http://dot.kr/x-test/creatingFile2.php

<php? header('Content-Type: text/html; charset=utf-8');?>

hello, johnyboy, did you the code above in your server?

well I don’t know then. There’s no guarantee my suggestion, when not giving the error, will solve your original problem. But it seems reasonably likely to me. It’ll tell browsers to display it correctly I think (this is assuming the data is actually correct). So far as being able to use header() properly I’m not sure what the problem is. Whenever you get that error it means that some sort of output has occurrred before the header() command, and because headers must come before normal output, php and/or the server, outputs what headers it has as soon as and before any output is to be output. Basically some output has taken place before your header() command. Where and what I don’t know. Maybe someone else can help?

> did you the code above in your server?

Yes I’ve used that line before for sure. It works, i.e. doesn’t give error.