Jump to content

Having problems with file manipulation which involves chinese named files


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.