Solved

Saving XML file w/ UTF-8

Posted on 2008-10-21
6
1,176 Views
Last Modified: 2008-10-21
I'm trying to save an xml file formatted in UTF-8 but it doesn't appear to be working. Here is a condensed version of my code:

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

mysql query goes here...

$xml .= "mysql xml output";

            $folder = $_SERVER['DOCUMENT_ROOT']."/path";
            
            if(!(is_dir($folder))) {
            mkdir($folder,0777);
            }


  $tempfile = $folder."/".date("Ymd").".xml";
  $fo = fopen($tempfile, 'w') or die("Cannot create XML file.");
  fwrite($fo,utf8_decode($xml));
  fclose($fo);

For some reason, I open the file and the UTF-8 characters are not recognized (seen as question marks). In the MySQL db, the collation is set to utf8_unicode_ci. Is there a way to set the file encoding when I am opening the file or writing to it?

0
Comment
Question by:sherlock5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22768492
why are you decoding UTF8 before writing to the file? Wouldn't you need to utf8_encode when saving?
0
 

Author Comment

by:sherlock5
ID: 22768555
i also tried utf8_encode... utf8_decode was the only option that limited the coding problem to just a question mark. I also switched the  encoding="iso-8859-1" without luck.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22768568
You seem to be decoding the utf8 before you write it to the file:

fwrite($fo,utf8_decode($xml));

Then you save it as latin1. It should be just:

fwrite($fo,$xml);

...or, if the $xml is latin1 and the file should be utf8:

fwrite($fo,utf8_encode($xml));
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:sherlock5
ID: 22768818
Yeah, cxr, I have tried all of those options as well. When I try to use the function, header("Content-type: application/octet-stream; charset=UTF-8");, to output the file in a new window, the content is formatted in UTF-8... so how do I save the file as a UTF-8 formatted file?
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 22769115
You say you open it and view it and see question marks, in what environment are you opening it? Through the web browser? If you view it in the browser and change character encoding in the browser, maybe you can identify what character encoding is in the file.

Your example says: $xml .= "mysql xml output";

I suppose the real $xml variable contains content grabbed directly from the database? Or is there any conversion involved? Is the character set used in the database utf8 (not just collation)? Execute this sql to find out:

show variables like "character_set%";
0
 

Author Comment

by:sherlock5
ID: 22769538
So, you were right cxr, I changed character_set_database to utf8... then i removed the utf8_encode... and then i opened the file in firefox and notepad, saw that the characters were showing up correctly and that the encoding was utf8. thanks for the help!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question