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();
順便說一句,每當'觸摸()'是成功的,最後修改時間通常會是'時間()':) –