php file()

Discussion in 'PHP' started by adamjblakey, Feb 22, 2011.

  1. #1
    Hi,

    I have a strange issue, i have a txt file on the server with the following text in:

    I am using the following to read the document:

    
           $file = $_SERVER['DOCUMENT_ROOT'].'/uploads/documents/test.txt'; 
    	$contents = file($file); 
    	$string = implode($contents); 
           print $string;
    
    PHP:
    But i am getting the below strange characters when i read and print what is in the file.

    Any ideas why this is happening?

    Cheers,
    Adam
     
    adamjblakey, Feb 22, 2011 IP
  2. Iwwaty

    Iwwaty Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,
    I suggest you to use readfile() or file_get_contents() to read file. It's very easy.
     
    Iwwaty, Feb 22, 2011 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I have tried readfile() and also file_get_contents() but both do the same thing. Any ideas?
     
    adamjblakey, Mar 10, 2011 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    You need to use utf-8 encoding.
     
    crazyryan, Mar 10, 2011 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    When you create that file, insure that you save as UTF-8. It is probably the quotes you have (copied over from a website, office, open office, etc..)

    If you are saving the file directly from your website, then insure that the PHP script files are UTF-8 as well, it should solve the problem.
     
    ThePHPMaster, Mar 10, 2011 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Thanks for the replies, how do i save a txt file with encoding? I did not think this was possible?

    I have tried adding header('Content-Type: text/html; charset=utf-8'); to the page where the text file is read and entered into the database however when the file is echoed it works fine but when entered into the database i still have all the strange characters.
     
    adamjblakey, Mar 11, 2011 IP
  7. bledileka

    bledileka Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    try htmlenitites() to save and get the values like html char codes.
     
    bledileka, Mar 11, 2011 IP
  8. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #8
    add just after <?php

    header('Content-Type: text/html; charset=utf-8');
     
    G3n3s!s, Mar 11, 2011 IP
  9. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #9
    $ f i l e = $ _ S E R V E R [ ' D O C U M E N T _ R O O T ' ] . ' / u p l o a d s / d o c u m e n t s /
    t e s t . t x t ' ;
    $ c o n t e n t s = f i l e_get_contents ( $ f i l e ) ;
    $replace=preg_replace("*'*","\'",$contents);
    echo nl2br( $replace);
     
    spaceman12, Mar 13, 2011 IP
  10. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #10
    Before adding the insert query, run this query:

    mysql_query("SET NAMES utf8");

    Make sure that the fields also have UTF8 encoding to them (as well as the table).
     
    ThePHPMaster, Mar 13, 2011 IP