SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    character problem in file creating

    I have a creatingFile.php which has the code below.
    Code:
    creatingFile.php
    
    <?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.
    Code:
    creatingFile.php
    
    <?php 
    file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', 'koreanText'); 
    ?>
    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?

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It definitely sounds like a character encoding issue. You could try using utf8_encode() on the Korean text, eg:

    PHP Code:
    <?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
    Martin.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    > As I browse createdFile.php, I can see some broken characters instead of the text "koreanText".

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

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    utf8_encode('koreanText')
    It doesn't work, but worth to try, thank you marbean, anyway.

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    what do you mean exactly by "I browse createdFile.php"? how do you go about doing that?
    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".
    Code:
    file.php
    
    $myVar=1111;
    echo $myVar;

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try
    Code:
    header('Content-Type: text/html; charset=utf-8');
    at the start of your file

  7. #7
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    try
    Code:
    header('Content-Type: text/html; charset=utf-8');
    I put the above at the start of myFile "creaingFile.php".
    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?
    Code:
    creatingFile.php
    <?php  
    header('Content-Type: text/html; charset=utf-8');
    file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', utf8_encode('koreanText'));  
    ?>

  8. #8
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    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?
    Code:
    creatingFile.php
    <?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:

    Code:
    <?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.

  9. #9
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    headers need to execute before *any* (including even a single literal white space character for example) characters are output.
    I think I understand what you mean because I sometimes see the WARNING when I use "head("location: $goingTo_someWhere");".

    Quote Originally Posted by johnyboy View Post
    Code:
    <?php
    header('Content-Type: text/html; charset=utf-8');
    ?>creatingFile.php
    <?php  
    file_put_contents(dirname(__my_subDirectory__) . '/' . 'createdFile.php', utf8_encode('koreanText'));  
    ?>
    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".
    Code:
    <?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');".
    Code:
    creating2.php
    
    <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"?

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    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
    Code:
    <php? header('Content-Type: text/html; charset=utf-8');?>
    hello, johnyboy, did you the code above in your server?

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    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
    Code:
    <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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •