2013-02-04 105 views
1

我需要在一個PHP腳本中觸摸一個文件,並在上次從另一個腳本中讀取此文件時,但無論如何觸摸該文件並讀出修改日期,修改日期都不會更改,下面是一個測試文件。如何觸摸文件並在Linux上讀取PHP中的修改日期?

如何觸摸日誌文件並更改修改日期,然後讀取此修改日期?

class TestKeepAlive { 

    protected $log_file_name; 

    public function process() { 
     $this->log_file_name = 'test_keepalive_log.txt'; 
     $this->_writeProcessIdToLogFile(); 

     for ($index = 0; $index < 10; $index++) { 
      echo 'test' . PHP_EOL; 
      sleep(1); 
      touch($this->log_file_name); 
      $this->_touchLogFile(); 
      $dateTimeLastTouched = $this->_getDateTimeLogFileLastTouched(); 
      echo $dateTimeLastTouched . PHP_EOL; 
     } 
    } 

    protected function _touchLogFile() { 
     //touch($this->log_file_name); 
     exec("touch {$this->log_file_name}"); 
    } 

    protected function _getDateTimeLogFileLastTouched() { 
     return filemtime($this->log_file_name); 
    } 

    protected function _writeProcessIdToLogFile() { 
     file_put_contents($this->log_file_name, getmypid()); 
    } 

} 

$testKeepAlive = new TestKeepAlive(); 
$testKeepAlive->process(); 
+0

順便說一句,每當'觸摸()'是成功的,最後修改時間通常會是'時間()':) –

回答

3

您應該使用在PHP Manual

PHP中發現的功能clearstatcache緩存信息,以便 提供 更快的性能,這些功能(filemtime)的回報。但是,在某些情況下,您可能需要清除緩存的 信息。例如,如果在一個 單個腳本中多次檢查同一個文件,並且該文件可能在 腳本的操作中被刪除或更改,您可以選擇清除狀態緩存。在這些情況下,您可以使用clearstatcache()函數清除PHP緩存文件的信息。

功能:

protected function _getDateTimeLogFileLastTouched() { 
    clearstatcache(); 
    return filemtime($this->log_file_name); 
} 
相關問題