16

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');

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:

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

everything works fine!

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

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

but it has not helped.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Benny Code
  • 51,456
  • 28
  • 233
  • 198
  • Do you have the same charset in the file as in your operating system? – Emil Vikström Oct 03 '11 at 18:52
  • 2
    Given it's a windows system, it's almost certainly NTFS, which is utf-16. – Marc B Oct 03 '11 at 18:54
  • At the moment I use this script on a Windows 7 machine (with an NTFS formatted hard disk) but later I want to use it on an Ubuntu server. – Benny Code Oct 03 '11 at 19:01
  • 1
    Try a `scandir()` and report the raw (`bin2hex`) character string of the picture. Could be that you're out of luck though http://stackoverflow.com/questions/482342/php-readdir-problem-with-japanese-language-file-name – mario Oct 03 '11 at 19:14
  • Can you influence the way the files are named? URLEncoding the file names before storing would be an easy way out, for example - you would be guaranteed to have a file name that works on every system, and a `urldecode()` call can give you back the full string, no problem. – Pekka Oct 03 '11 at 19:15
  • Thank you for the quick replies! The scandir() function shows "M�nchen.JPG" which is "4df66e6368656e2e4a5047" with bin2hex. Unfortunately I can not simply rename the files so they must keep their names. – Benny Code Oct 03 '11 at 19:36

2 Answers2

12

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;

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

Benny Code
  • 51,456
  • 28
  • 233
  • 198
1

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);
        }
    }
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Hung Ngo
  • 11
  • 1