0

I'm pulling contents from text files into a textarea to be used and noticed it appeared that slashes were appearing near quotes and apostrophes. I was able to resolve that by disabling magic quotes on the server, however I noticed that special characters still don't seem to display properly.

What I am trying to figure out is there a way when retrieving the file to decode/encode them properly or to encode them so they're UTF 8 compliant in the first place? Below is my coding for retrieving the files:

<?php
    $directory = $directory = 'users/' . $_SESSION['username'];
    $filesContents = Array();
    $files = scandir( $directory ) ;

    foreach( $files as $file ) {
        if ( ! is_dir( $file ) ) {
            $filesContents[$file] = file_get_contents($directory , $file);
            echo '<option value="'. $file .'">' . $file . '</option>';
        }
    }
?>
</select>

and below is my save script:

if($_POST['Action'] == "SAVE") {
    //  If a session already exists, this doesn't have any effect.
    session_start();

    //  Sets the current directory to the directory this script is running in
    chdir(dirname(__FILE__));

    //  Breakpoint
    if( empty($_SESSION['username']) || $_SESSION['username'] == '' ) {
        echo 'There is no session username';
    }
    if( empty($_POST['CodeDescription']) || $_POST['CodeDescription'] == '' ) {
        echo 'There is no POST desired filename';
    }

    //  This is assuming we are working from the current directory that is running this PHP file.
    $USER_DIRECTORY = 'users/'.$_SESSION['username'];

    //  Makes the directory if it doesn't exist
    if(!is_dir($USER_DIRECTORY)):
        mkdir($USER_DIRECTORY);
    endif;

    //  Put together the full path of the file we want to create
    $FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.txt';
    if( !is_file( $FILENAME ) ):
        // Open the text file, write the contents, and close it.
        file_put_contents($FILENAME, $_POST['Code']);
    endif;
    header('Location: mysite.site/evo/codesaveindex.php?saved=1&file='.$FILENAME);
}
?>
prava
  • 3,916
  • 2
  • 24
  • 35
Robert Ettinger
  • 319
  • 1
  • 3
  • 17
  • Do you have `` in your ``? – Tschallacka Mar 16 '15 at 07:06
  • Just doublechecked all of my pages, and yes. there was one page missing it, but didn't change anything. The only place I don't have it is in my php script save file. – Robert Ettinger Mar 16 '15 at 07:18
  • And your form code has accept-charset="UTF-8"? and for good measure you could use utf8_encode() on the posted code. That way the whole chain is utf8 what you controll http://php.net/manual/en/function.utf8-encode.php and to go even one step better add this before any content in your php files header('Content-Type: text/html; charset=utf-8'); See if that resolves anything... – Tschallacka Mar 16 '15 at 07:26
  • Thanks Michael I added accept-charset="UTF-8" to my form which seemed to fix it but only for one file. To clarify a couple of things, where exactly would I put in my code string utf8_encode(). Am trying to figure that out, also to further clarify did you mean put the meta tag on the page with just my php processing code? or the page that calls it? – Robert Ettinger Mar 16 '15 at 08:22
  • `file_put_contents($FILENAME, utf8_encode($_POST['Code']));` And the meta tag should be on any page that outputs HTML to the user. The best option is the `header('Content-Type: text/html; charset=utf-8');` at the very beginning of your php file after your session start. That way all communications will be utf, and hte meta tag is for browsers that miss that for some reason(or if you are on a windows apache :S) – Tschallacka Mar 16 '15 at 08:35
  • Thanks michael. I made those changes (and appreciate that link) though its still not fixing it for all files. I'm reading that something in file_get_contents where I pull the file from might break up UTF8 encoding is there a way to keep that in tact or might that be the issue? – Robert Ettinger Mar 16 '15 at 09:23
  • Thanks for all of your help michael. Im' not sure what was going on but I changed from UTF-8 to iso-8859-1 and it all seems to work. I appreciate all of the tips and the help. – Robert Ettinger Mar 16 '15 at 10:04
  • Need to change that comment. After further testing its still not working. I still get special characters when all browsers are showing its picking up UTF 8 encoding. – Robert Ettinger Mar 16 '15 at 11:37
  • You might want to try some things in this duplicate http://stackoverflow.com/questions/2236668/file-get-contents-breaks-up-utf-8-characters – Tschallacka Mar 16 '15 at 12:01

0 Answers0