简体   繁体   English

readdir和scandir

[英]readdir and scandir

On my localhost machine, I have two files : 在我的localhost机器上,我有两个文件:

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

I want to get filename of themes by scandir() or readdir() but can't get extractly filename. 我想通过scandir()或readdir()获取主题的文件名,但不能得到精简文件名。 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 ? 我如何获得像IMG_9029_измен.размер.jpg and słońce32 opalenier1.jpg这样的文件名?

I assume that your php encoding is utf-8. 我假设你的php编码是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. 主要问题是php 5不支持utf-8进行文件操作。 It is internally ansi. 它是内部ansi。 So it reads file names using ansi api (only 8bit encoding). 所以它使用ansi api(只有8位编码)读取文件名。

The names are probably right, but you need to set the encoding of your page to UTF-8. 名称可能是正确的,但您需要将页面的编码设置为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. 然后将您的字符串转换为UTF8,以便正确显示特殊字符。

I made a function that does it, it's called Encoding::toUTF8(). 我创建了一个函数,它被称为Encoding::toUTF8().

Usage: 用法:

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

Download: 下载:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM