Jump to content

Having problems with file manipulation which involves chinese named files


joe25

Recommended Posts

Hello guys I hope you can help me with this one.

Whenever im using rename(),copy() or fwrite() on chinese named files, I get an error or weird results.

 

When Im trying to rename an english named file, for example "work.html" to "指南.html", I get a file named "指å—.html" instead.

 

Here is my code:

 

<?
//Used so the chinese characters would show up properly on this page. Remove this and the character encoding goes weird. UTF is the encoding I use for simple chinese characters.
ini_set('default_charset', 'UTF-8');

//this really doesn't do anything, just tried to make use of the third value for rename() function
$opts = array(
'http'=>array(
//'method'=>"GET",
'header'=>"default_charset = UTF-8"
)
);
$context = stream_context_create($opts);

//Chinese character in hex
$xslt_result = "&#25351;&#21335;";
//Convert the hex to chinese characters
$work = html_entity_decode($xslt_result, ENT_NOQUOTES,'UTF-8');

print $work.".html should be the resulting file";

rename("work.html", $work.".html",$context);
//OR just - rename("work.html", "指南.html",$context);
?>

 

When I try to use the copy() or fwrite() function I get an error instead. rename() is the only php function that is somewhat working with SIMPLE CHINESE characters. (i mean not traditional)

Im using windows vista with XAMPP.

I hope someone can help me with this. I would greatly appreciate it.

 

*note: create a blank file named work.html before running my code*

You have to have an utf8_encode() function within your fwrite command.. Example:

//Convert the hex to chinese characters
$work = html_entity_decode($xslt_result, ENT_NOQUOTES,'UTF-8');
//$work = utf8_encode($work);

print $work.".html should be the resulting file";

rename("work.html", utf8_encode($work).".html",$context);

Try and see if that works.. fwrite and rename seem to default to ISO-latin1..

I got this result instead "指å—.html"

 

Anyway, yes I think that ISO-latin1 is the default encoding for PHP file manipulation functions. I think the third item for the rename() item is the key. I just can't seem to use it right.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.