0

I want to save a file to Windows using Japanese characters in the filename.

The PHP file is saved with UTF-8 encoding

<?php
$oldfile = "test.txt";
$newfile = "日本語.txt";

copy($oldfile,$newfile);
?>

The file copies, but appears in Windows as

日本語.txt

How do I make it save as

日本語.txt

?

Joe
  • 677
  • 1
  • 6
  • 13
  • Hi hi, looking forward to the discussion on character encoding on a MS-Windows platform, especially if it comes to the file system part... I wonder what hero will attempt to step forward and guide through that jungle called an "operating system" :-) – arkascha Sep 19 '16 at 18:30

2 Answers2

0

I have ended up using the php-wfio extension from https://github.com/kenjiuno/php-wfio

After putting php_wfio.dll into php\ext folder and enabling the extension, I prefixed the filenames with wfio:// (both need to be prefixed or you get a Cannot rename a file across wrapper types error)

My test code ends up looking like

<?php
$oldfile = "wfio://test.txt";
$newfile = "wfio://日本語.txt";

copy($oldfile,$newfile);
?>

and the file gets saved in Windows as 日本語.txt which is what I was looking for

Joe
  • 677
  • 1
  • 6
  • 13
0

Starting with PHP 7.1, i would link you to this answer https://stackoverflow.com/a/38466772/3358424 . Unfortunately, the most of the recommendations are not valid, that are listed in the answer that strives to be the only correct one. Like "just urlencode the filename" or "FS expects iso-8859-1", etc. are terribly wrong assumptions that misinform people. That can work by luck but are only valid for US or almost western codepages, but are otherwise just wrong. PHP 7.1 + default_charset=UTF-8 is what you want. With earlier PHP versions, wfio or wrappers to ext/com_dotnet might be indeed helpful.

Thanks.

Anatol Belski
  • 660
  • 6
  • 8