7

On my localhost machine, I have two files :

IMG_9029_измен.размер.jpg and słońce32 opalenier1.jpg

I want to get filename of themes by scandir() or readdir() but can't get extractly filename. They are like this:

IMG_9029_?????.??????.jpg  and slonce32 opalenier.jpg

Window XP SP3, php5.2.12 

How I can get filename like IMG_9029_измен.размер.jpg and słońce32 opalenier1.jpg ?

Bakudan
  • 19,134
  • 9
  • 53
  • 73
Chameron
  • 2,734
  • 8
  • 34
  • 46
  • Is your filename in unicode? and when you output the filenames to your webpage, is the webpage encoding in unicode too? – minghan Aug 23 '10 at 04:57
  • http://stackoverflow.com/questions/977635/how-to-open-file-in-php-that-has-unicode-characters-in-its-name – Bakudan Dec 29 '11 at 00:58

2 Answers2

8

I assume that your php encoding is utf-8.

$files = scandir($dirname);
foreach ($files as $file) {
    $filename = iconv ( "windows-1251" , "UTF-8", $file );
    echo $filename ."\n";
}

It should work for first file, but I don't know which encoding use for second file.

The main problem is that php 5 don't support utf-8 for file operations. It is internally ansi. So it reads file names using ansi api (only 8bit encoding).

Tomas
  • 1,531
  • 1
  • 16
  • 22
1

The names are probably right, but you need to set the encoding of your page to UTF-8.

Add this to the header section of your pages:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

Then convert your strings to UTF8 so the special characters show up correctly.

I made a function that does it, it's called Encoding::toUTF8().

Usage:

$utf8_string = Encoding::toUTF8($utf8_or_latin1_or_mixed_string);

Download:

http://dl.dropbox.com/u/186012/PHP/forceUTF8.zip

Saty
  • 22,443
  • 7
  • 33
  • 51
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
  • 1
    thank for suggestion.filename was changed to IMG_9029_?????.??????.jpg when i use readdir() or scandir(). how to encode with this name.I think it is not possible – Chameron Aug 24 '10 at 02:18