开发者

readdir and scandir

开发者 https://www.devze.com 2023-01-13 03:37 出处:网络
On my localhost machine, I have two file开发者_运维百科s : IMG_9029_измен.размер.jpg and słońce32 opalenier1.jpg

On my localhost machine, I have two file开发者_运维百科s :

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 ?


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消