Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9231101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:59:14+00:00 2026-06-18T05:59:14+00:00

I need to touch a file from within one PHP script and read the

  • 0

I need to touch a file from within one PHP script and read the last time this file was touched from within another script, but no matter how I touch the file and read out the modification date, the modification date doesn’t change, below is a test file.

How can I touch the log file and thus change the modification date, and then read this modification date?

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:59:16+00:00Added an answer on June 18, 2026 at 5:59 am

    You should use the function clearstatcache found in the PHP Manual

    PHP caches the information those functions(filemtime) return in order
    to provide
    faster performance. However, in certain cases, you may want to clear the cached
    information. For instance, if the same file is being checked multiple times within a
    single script, and that file is in danger of being removed or changed during that
    script’s operation, you may elect to clear the status cache. In these cases, you can
    use the clearstatcache() function to clear the information that PHP caches about a file.

    Function:

    protected function _getDateTimeLogFileLastTouched() {
        clearstatcache();
        return filemtime($this->log_file_name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing my own OS, but for this I need to touch on linking,
I have a basic PHP script like this: <?php system(/bin/touch /root/testfile); When I run
I need to write a shell script where I read a variable from environment.
I'm catching touch events. I need to differentiate two events 1) User touched a
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I'm trying to reference another Xcode4 project within Xcode4 but having troubles - what's
I'm working on an application in C# and need to read and write from
What's a batch script command to touch on a file to update its date
This is a tough one, but I am sure it is not unheard of.
I need help with jQuery and ajax. need to load textarea content from file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.