Page 1 of 1

PHP Experts...

Posted: Thu 01 Dec 2011 10:47 am
by mcrossley
I want PHP script that once invoked by a client..

1. Runs in a loop, with a delay - I can do this bit!
2. Each time round the loop checks if a file on the web server has been updated - how?
3. If updated, reads the file - OK, normal fopen() stuff.
4. Dos some 'stuff' with the contents - OK.

So step 2, I assume I would use filectime(), but to save me the work (I'm no PHP coder) does anyone have a bit of precanned code to do this?

Re: PHP Experts...

Posted: Thu 01 Dec 2011 11:22 am
by gemini06720
Mark, I am certainly no expert either... :mrgreen:

You can certainly use the change time of file (filectime) and possibly the file modification time (filemtime).

But, for both of those functions properly, you will need to keep a pointer (such as a small file) where the last time check will be written to - it would then be a matter of reading the pointer file (with the previously recorded update time information) and comparing its information with either the 'filectime' (or 'filemtime') of the file to be checked/watched (updated).

In one of my scripts, I check the number of minutes that have elapsed between the time a METAR file has been downloaded and save into a cache directory and the present time the script is running - if that time exceed a pre-determine number of minutes, then the script download (and save) a new copy of the METAR file. I understand that is not exactly what you need, but that might 'generate' some ideas/suggestions... ;)

Here is an excerpt the code used in that script:

Code: Select all

  $metarRefresh = 20; // time between refresh, in minutes
  $download = true; // set pointer to download a fresh copy of the file
  if (file_exists($metarFile)) { // check that the metar file already exist
    $metarTime  = filemtime($metarFile); // get the last modification time of the metar file
    $reloadTime = $metarTime + ($metarRefresh*60); // refresh time set to 1200 seconds - check time difference
    $now = mktime(); // get the time now
    if ($now < $reloadTime) { // has the time been exceeded
      $metar = getTxtFile($metarFile); // if not, get/read the saved file from the cache
      $download = false; // set pointer to not download a fresh copy of the file
    }
  }
And further down, I have something like:

Code: Select all

  if ($download) { // download a fresh copy of the METAR file if that file is stale or missing
.....
  }

Re: PHP Experts...

Posted: Thu 01 Dec 2011 11:40 am
by mcrossley
Thanks Ray, I was thinking my script will only need to track the file change time for the duration of the of the script execution - which I plan to limit to 1 minute, at which point the client would automatically re-invoke the script if required. But thinking about this it may be more complicated than I first thought, as each invocation of the script may or may not be by the same client.

That is the script would have to 'know' if this was the first time it was being called for a client, or if it was being re-invoked after a time out.

I may resort to performing the update check at the client side, that removes the need for session tracking on the server.

Re: PHP Experts...

Posted: Thu 01 Dec 2011 11:50 am
by gemini06720
Or you could check from where (whom) the request for the script is coming from...

Client side might be a better way of doing this ... although I really dislike 'imposing' on the client unless absolutely needed (no other way to do it)...

Re: PHP Experts...

Posted: Thu 01 Dec 2011 4:43 pm
by beteljuice
The beteljuice would use filemtime, and the methodology of the 'cachefile' script.

You don't say if it is necessary for the file to be processed every minute, or 'latest' on-demand.
If the former, the cachfile code would be unsuitable.

Re: PHP Experts...

Posted: Thu 01 Dec 2011 10:43 pm
by mcrossley
my idea was to process the file as soon as it was updated, so put a sleep in the detection loop of about 1 sec, then push it out to the client. Thinking realtime.txt here.

Re: PHP Experts...

Posted: Mon 02 Jan 2012 5:03 am
by serowe
Only just read this - you need to be VERY careful your script doesn;t run away with itself and hog the servers CPU. Been there, done that - and then you find out how good (or bad!) your ISP really is with errant users :)

Re: PHP Experts...

Posted: Mon 02 Jan 2012 11:00 am
by mcrossley
I decided that it was too complex, and not enough browsers implement push technology yet, so reverted to good old Ajax