Reading and scandir

On my localhost machine, I have two files:

IMG_9029_..jpg and słońce32 opalenier1.jpg

I want to get the file name scandir () or readdir () , but I can not get the extracted file name. They look like this:

 IMG_9029_?????.??????.jpg and slonce32 opalenier.jpg Window XP SP3, php5.2.12 

How can I get the file name, for example IMG_9029_..jpg and słońce32 opalenier1.jpg ?

+7
php
source share
2 answers

I assume 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 the first file, but I don't know what encoding is used for the second file.

The main problem is that php 5 does not support utf-8 for file operations. This is internally annecy. Therefore, it reads the file names using ansi api (only 8-bit encoding).

+8
source share

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

Add this to your page title:

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

Then convert your strings to UTF8 so that special characters are displayed correctly.

I made a function that does this, it is called Encoding::toUTF8().

Using:

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

Download:

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

+1
source share

All Articles