Author Topic: How to create a file with default charset utf-8  (Read 1927 times)

0 Members and 1 Guest are viewing this topic.

Offline gibaTopic starter

  • Enthusiast
  • Posts: 55
  • Gender: Male
  • Gilberto Albino
    • View Profile
How to create a file with default charset utf-8
« on: December 22, 2008, 12:29:05 PM »
Hi there, guys.

Hey I got an issue today.

I am building a system that creates files on the fly
But I must use utf-8 as default charset,
because it is used for the most application files.
In fact I am doing the basic task.

He is the basic code:

if(file_exists($file)) {
   $error = "This is already exists!";            
} else {
   /**   
    *   Open the file
   */
     $f= fopen($file,'x+');
       
   /**
    *   Write the new content
   */
     fwrite($f,$content);

   /**
    *   Close the file
   */
   fclose($f);            
}

How could I generate this file in utf-8?

Giba

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: How to create a file with default charset utf-8
« Reply #1 on: December 22, 2008, 12:59:08 PM »
How could I generate this file in utf-8?
Assuming that the actual data you want to write includes multi-byte characters, simply write the data to that file.
It's the external applications used to read that file that need to support utf-8.
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline gibaTopic starter

  • Enthusiast
  • Posts: 55
  • Gender: Male
  • Gilberto Albino
    • View Profile
Re: How to create a file with default charset utf-8
« Reply #2 on: December 22, 2008, 01:20:33 PM »
Well, I have done this yet!
But to make it clear,
When the server creates a file on Windows it does as ANSI.
On Linux it does as UTF-8.

So when I include a ANSI file inside a UTF-8 it gerenates strange characters.

The workaround is manually converting it to utf-8 on Windows with notepad++,
on Linux it's all right.

But this is an important issue because the application must be cross platform.

I will let it as it is!
Giba

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: How to create a file with default charset utf-8
« Reply #3 on: December 22, 2008, 01:47:54 PM »
When the server creates a file on Windows it does as ANSI.
$fh = fopen("/home/rasmus/file.txt", "wb");
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline gibaTopic starter

  • Enthusiast
  • Posts: 55
  • Gender: Male
  • Gilberto Albino
    • View Profile
Re: How to create a file with default charset utf-8
« Reply #4 on: December 22, 2008, 04:59:22 PM »
Sure, that's all right now!
I haven't seen this before because I didn't looked at the note inside the PHP manual  ::).
The little I used file handling with PHP was that simple.
But, yes, in fact it now works perfectly.
Thanks.
Giba