带有变音符号的文件名的统计信息失败

带有变音符号的文件名的统计信息失败

本文介绍了filemtime()[function.filemtime]:带有变音符号的文件名的统计信息失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP函数 filemtime 来获取最后的修改时间使用PHP 5.3.此功能效果很好,但是当文件名包含特殊字符(例如变音符)时,似乎会出现一些问题.

I use the PHP function filemtime to 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).

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

If I run it on a filename with umlauts

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

然后我得到输出:

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

如果我将文件从München.JPG"重命名为"Muenchen.JPG",然后再次执行相同的操作:

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

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

一切正常!

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

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

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

但没有帮助.

推荐答案

使用以下代码段,我发现Windows 7上的文件编码为"ISO-8859-1":

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

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

我已经阅读到 utf8_decode 会转换为UTF- ISO-8859-1的8个字符串,因此我得到了适用于我的项目的以下小代码:

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

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

这篇关于filemtime()[function.filemtime]:带有变音符号的文件名的统计信息失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:43