Forum closed. New forum available at http://community.wymeditor.org/

PHP fwrite with another encoding coming from WYM

Support forum for WYMeditor.

PHP fwrite with another encoding coming from WYM

Postby jf_r on Fri Mar 23, 2007 12:44 pm

Hi everyone,

I've successfully implemented a CMS system based on WYM, everything is working ok and I can modify txt includes and save them to the server user fwrite.

Server is an apache box, with PHP 4 installed.

What I do is
sContent = $_POST['content'];

then I fwrite the string.

This work perfectly fine and the text get saved, except when there are special character such as é è à ê, etc.

Well actually those character do get saved, if I open the txt file I see them in the file however when I use PHP to include the file it show symbols instead of the character.

I'm guessing it has to do with some string encoding but I'm totally lost on this one...don't know where to start since the file does look ok when you open it....

Anyone ever seen something similar? Any help would be greatly appreciated.
--
jf_r
jf_r
 
Posts: 4
Joined: Fri Mar 23, 2007 12:35 pm

Postby Bravo_Kernel on Sat Mar 24, 2007 3:39 pm

Hi jf_r,

Try using the following code to read the contents of the file instead of plain "including" the file. Your special chars should be displayed.

Code: Select all
<?php
$filename = "/path/to/test.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo "Filecontents:<br />$contents";
?>


Please notice that on a Windows machine the fopen statement might need "rb" instead op "r". See http://nl2.php.net/manual/en/function.fread.php for full details.

HTH,
Bravo
Bravo_Kernel
 
Posts: 10
Joined: Sat Feb 10, 2007 1:52 pm

Postby jf_r on Sun Mar 25, 2007 5:34 pm

Hi Bravo,

Thanks for the answer, I'm trying it right now to see if it works...I'll edit this post with the results.

EDIT:

Ok, I've tried that...it didn't work. Here's the code that I have so far, hope it helps!

I have a file called rte.php which contain the wymeditor instance:

Code: Select all
require_once("library.php");

   $fileName = $_GET["file"];

   $lang = $_GET["lang"];
   
   //Set the full relative path
   if ($lang == "en")
      $filePath = "../includes/txt/";
   else
      $filePath = "../../fr/includes/txt/";      

   $fileToRead = $filePath . $fileName;
   
   if (file_exists($fileToRead)) {
      //Get the content of the file in a string
      //file(strPath) returns an array of string, implode concatenate it back to a single string.
      if ( $fileContent = implode('', file($fileToRead)) ){
         //
      }
      else{
            echo BuildErrorString($_SERVER['PHP_SELF'], "Error reading", true);
      }
   }
   else {
         echo BuildErrorString($_SERVER['PHP_SELF'], "The file does not exist", true);
   }   


THis correctly read the file passed as a parameter, I then load the string $fileContent in wymeditor and I see the file fine.

If I open a preview from wymeditor I see the special characters ok at this point. At the bottom there's a form submit which submit to the script writing to the file:

Code: Select all
$filename = $_POST['filename'];

$fp = fopen( $filename, "w" );

if(!$fp)
{echo "Couldn't open the data file. Try again later.";
exit;}

$content = $_POST['txthtml'];

//echo $content;

fwrite( $fp, $content );
fclose( $fp );

header("Location: mg_text.php");


This also correctly write to the file and the special characters are in the file when I open it.

Now when I try going to the page which make use of the txt file it shows "garbage" characters instead.

I used to use a simple include(foobar.txt);

Then I tried as you suggested:

Code: Select all
$filename = "includes/txt/contact.txt";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;


This still shows "garbage" instead of the actual characters... :( I'm kinda lost on this one...
--
jf_r
jf_r
 
Posts: 4
Joined: Fri Mar 23, 2007 12:35 pm

Postby Bravo_Kernel on Sun Mar 25, 2007 9:21 pm

Lo again m8,

While I am waiting for the new WYMEditor release I don't really have anything better to do so I'll put some effort into your problem ;)

Your code example differs from mine in that you are using "rb" instead of "r" in the second line. Have you tried "r" as well? The b is for binary files and only needed for Windows environments if I remember correctly.

If that doesn't work please PM me the (link to your) contact.txt file that is giving you problems and I will get back to you.

HTH
Bravo_Kernel
 
Posts: 10
Joined: Sat Feb 10, 2007 1:52 pm

Postby jf_r on Mon Mar 26, 2007 2:06 pm

Hi again,

Thanks a lot for the help, I'm at work right now but will get back to you as soon as I get back...
--
jf_r
jf_r
 
Posts: 4
Joined: Fri Mar 23, 2007 12:35 pm

Postby Bravo_Kernel on Mon Apr 02, 2007 7:17 pm

Hi m8,

A bit late, I've been quite busy, but here it is: you have to decode the garbled characters once you have read them back in from your textfile.

fwrite:
Code: Select all
<?php
$myFile = "/path/to/accents.txt";
$myString = "Test with accents éèàç";

$fh=fopen($myFile, "w") or die ("Error opening $myFile");
fwrite($fh,utf8_encode($myString));
fclose($fh);
?>


fopen:
Code: Select all
<?php
$filename = "/path/to/accents.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));

$decoded = utf8_decode($contents);

fclose($handle);
echo "Contents = $contents<br />";
echo "Decoded = $decoded<br />";
?>


Hope your OK now. Good luck with your CMS, make something nice of it.
Hopefully I can start working on miine too soon. Still no WYM update :cry:
Bravo_Kernel
 
Posts: 10
Joined: Sat Feb 10, 2007 1:52 pm

Postby jf_r on Wed Apr 04, 2007 12:35 am

Awesome! That work just fine :)

/me buy a good cold draught for you (or whatever else if you don't like beer ;))
--
jf_r
jf_r
 
Posts: 4
Joined: Fri Mar 23, 2007 12:35 pm


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest