Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to create unicode filenames

I'm trying to create files with Unicode characters in there filenames. I don't quite know what encoding I should use, or if it's possible at all.

I have this file, saved in latin1 encoding:

$h = fopen("unicode_♫.txt", 'w');
fclose($h);

In UTF-8 this would decode as 'unicode_♫.txt'. It writes it in the latin1 version to the disk (which is obvious?). I need it to be save as it would appear with UTF-8 decoding. I've also tried encoding it with UTF-16 but that's not working either.

I'm using PHP 5.2, and would like this to work with NTFS, ext3 and ext4.

How can this be done?

like image 633
T-RonX Avatar asked Jun 24 '11 12:06

T-RonX


2 Answers

It can't currently be done on Windows (possibly PHP 5.4 will support this scenario). In PHP, you can only write filenames using the Windows set codepage. If the codepage, does not include the character , you cannot use it. Worse, if you have a file on Windows with such character in its filename, you'll have trouble accessing it.

In Linux, at least with ext*, it's a different story. You can use whatever filenames you want, the OS doesn't care about the encoding. So if you consistently use filenames in UTF-8, you should be OK. UTF-16 is however excluded because filenames cannot include bytes with value 0.

like image 117
Artefacto Avatar answered Oct 17 '22 07:10

Artefacto


for me the code below works well on Win7/ntfs, Apache 2.2.21.0 & PHP 5.3.8.0:

<?php
// this source file is utf-8 encoded

$fileContent = "Content of my file which contains Turkish characters such as şığŞİĞ";

$dirName = 'Dirname with utf-8 chars such as şığŞİĞ';
$fileName = 'Filename with utf-8 chars such as şığŞİĞ';

// converting encodings of names from utf-8 to iso-8859-9 (Turkish)
$encodedDirName = iconv("UTF-8", "ISO-8859-9//TRANSLIT", $dirName);
$encodedFileName = iconv("UTF-8", "ISO-8859-9//TRANSLIT", $fileName);

mkdir($encodedDirName);
file_put_contents("$encodedDirName/$encodedFileName.txt", $fileContent);

you can do same thing for opening files:

<?php
$fileName = "Filename with utf-8 chars such as şığ";
$fileContent = file_get_contents(iconv("UTF-8", "ISO-8859-9//TRANSLIT", "$fileName.txt"));
print $fileContent;
like image 28
hersly Avatar answered Oct 17 '22 07:10

hersly