php filemtime() [function.filemtime]:对于带有变音符号的文件名,统计失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7639292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:05:02  来源:igfitidea点击:

filemtime() [function.filemtime]: stat failed for filenames with umlauts

phputf-8statisticsdiacriticsfilemtime

提问by Benny Neugebauer

I use the PHP function filemtimeto get the last modification time with PHP 5.3. This functions works very well but it seems to have some problems when the filenames have special characters (for example umlauts).

我使用 PHP 函数filemtime来获取 PHP 5.3 的最后修改时间。此函数运行良好,但当文件名包含特殊字符(例如元音变音)时,它似乎会出现一些问题。

If I run it on a filename with umlauts

如果我在带有变音符号的文件名上运行它

$stat = filemtime('C:/pictures/München.JPG');

then I get the output:

然后我得到输出:

Warning: filemtime() [function.filemtime]: stat failed for C:/pictures/München.JPG

If I rename the file from "München.JPG" to "Muenchen.JPG" and do the same thing again:

如果我将文件从“München.JPG”重命名为“Muenchen.JPG”并再次执行相同的操作:

 $stat = filemtime('C:/pictures/Muenchen.JPG');

everything works fine!

一切正常!

My PHP file is saved as UTF-8 without BOM and I also tried:

我的 PHP 文件保存为没有 BOM 的 UTF-8,我也尝试过:

clearstatcache();
$stat = filemtime(utf8_encode('C:/pictures/München.JPG'));

but it has not helped.

但它没有帮助。

采纳答案by Benny Neugebauer

With the following code snippet I found out that the file encoding on Windows 7 is "ISO-8859-1":

通过以下代码片段,我发现 Windows 7 上的文件编码为“ISO-8859-1”:

$scandir = scandir('.')
$encoding = mb_detect_encoding($scandir[0], 'ISO-8859-1, UTF-8, ASCII');
echo $encoding;

I've read that utf8_decodeconverts a UTF-8 string to ISO-8859-1 so I ended up with this small code that works for my project:

我读过utf8_decode将 UTF-8 字符串转换为 ISO-8859-1 所以我最终得到了这个适用于我的项目的小代码:

$file = 'C:/pictures/München.JPG';
$lastModified = @filemtime($file);
if($lastModified == NULL)
    $lastModified = filemtime(utf8_decode($file));
echo $lastModified;

Thank you to all who have submitted a comment. You have steered me in the right direction. :-)

感谢所有提交评论的人。你把我引向了正确的方向。:-)

回答by Hung Ngo

try this

尝试这个

$dir    = 'uploads/';

        if (is_dir($dir)) { if ($dh = opendir($dir)) {

            while (($file = readdir($dh)) !== false) {                
                clearstatcache();
                if(is_file($dir."/".$file)) {                    
                    echo $file;
                    echo " - ";                    
                    echo "Last modified: " . date ("F d, Y H:i:s.", filemtime(utf8_decode($dir."/".$file)));
                    echo "<br>";
                }                
            }            

            echo "<br>";
            closedir($dh);
        }
    }