Search code examples
phplong-polling

PHP long poll bug


I'm trying to learn long poll and I'm using following PHP script:

<?php
$lastMod = filemtime('test.txt');

$counter = 10;
while ($counter > 0) {
    if (filemtime('test.txt') !== $lastMod) {
        echo file_get_contents('./test.txt');
        exit;
    }
    $counter--;
    sleep(1);
}

echo $lastMod;

But no matter what I do or try, it doesn't do anything when I change test.txt content during script execution.

I would be really happy if somebody told me where is the mistake.


Solution

  • The filemtime result is cached during runtime, so you need to reset it explicitly using clearstatcache().

    Run it right before your if.