Milisaniye PHP dosyası değişiklik zamanı

4 Cevap php

Orada, ben şu anda bir dosya değiştirilmiş alamadım iddia bir birim test yazıyorum. Test kod yürütme bir saniyeden daha az sürer ve milisaniye cinsinden dosya değiştirme zamanını almak mümkün olup olmadığını, bu nedenle ben bilmek istiyorum. filemtime () fonksiyonu saniye UNIX zaman damgası döndürür.

Benim geçerli çözüm 1 saniye bu değiştirilmiş ya da değil eğer kontrol etmeden geçti bana temin edecek (1) işlevi uyku kullanıyor. Büyük bir anlaşma ile testi yavaşlatır beri ben bu çözümü sevmiyorum.

Ben yeniden olabilir veri aynı olurdu çünkü get_file_contents üzerinden içerik eşitlik () iddia edemez.

Ben, imkansız öyle tahmin ediyorum?

4 Cevap

Bu basit komutu deneyin:

ls --full-time 'filename'

and you can see the file timestamp precision is not second, it is more precise. (using Linux, but don't think it differs in Unix) but I still don't know of a PHP function for getting precise timestamp, maybe you can parse the result of the system call.

Dosya sistemi ext4 (Ubuntu gibi daha yeni Unix / Linux dağıtımları ortak) veya ntfs (Windows) ise, mtime var alt-ikinci kesinlik .

Dosya sistemi ext3 (veya belki de diğerleri, bu bir süre önce standart oldu ve hala RHEL tarafından kullanılan) ise, mtime sadece yakın ikinci saklanır. Belki o eski varsayılan neden PHP sadece destekleri mtime yakın ikinci.

PHP değerini almak için, PHP'nin kendisi bunu desteklemediği, harici util aramak gerekir.

(Ben sadece bir İngilizce yerel bir sistemde aşağıdaki test ettik, ve "okunabilir insan" çıktı stat farklı olabilir, ya da strtotime davranış İngilizce olmayan yerel farklı olabilir. Bu, herhangi bir zaman dilimi içinde iyi çalışır çıktısı olarak stat strtotime tarafından onurlandırıldı bir zaman dilimi belirtici içerir.) olmalıdır

class FileModTimeHelper
{
    /**
     * Returns the file mtime for the specified file, in the format returned by microtime()
     *
     * On file systems which do not support sub-second mtime precision (such as ext3), the value
     * will be rounded to the nearest second.
     *
     * There must be a posix standard "stat" on your path (e.g. on unix or Windows with Cygwin)
     *
     * @param $filename string the name of the file
     * @return string like microtime()
     */
    public static function getFileModMicrotime($filename)
    {
        $stat = `stat --format=%y $filename`;
        $patt = '/^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)\.(\d+) (.*)$/';
        if (!preg_match($patt, $stat, $matches)) {
            throw new \Exception("Unrecognised output from stat. Expecting something like '$patt', found: '$stat'");
        }
        $mtimeSeconds = strtotime("{$matches[1]} {$matches[3]}");
        $mtimeMillis = $matches[2];
        return "$mtimeSeconds.$mtimeMillis";
    }
}
function getTime($path){
    clearstatcache($path);
    $dateUnix = shell_exec('stat --format "%y" '.$path);
    $date = explode(".", $dateUnix);
    return filemtime($path).".".substr($date[1], 0, 8);
}

getTime ("myTestTile");