PHP filesize() showing old filesize with a file inside a windows shared (network) folder

Viewed 145

I have the following script that runs to read new content from a file:

<?php
clearstatcache();
$fileURL = "\\\\saturn\extern\seq_ws.csv";
$fileAvailable = file_exists($fileURL);
$bytesRead = file_get_contents("bytes.txt");

if($fileAvailable){
    $fileSize = filesize($fileURL);
    //Statusses 1 = Partial read, 2 = Complete read, 0 = No read, -1 File not found. followed by !!
    if($bytesRead < $fileSize){
        //$bytesRead till $fileSize bytes read from file.
        $content = file_get_contents($fileURL, NULL, NULL, $bytesRead);
        file_put_contents("bytes.txt", ((int)$bytesRead + strlen($content)));
        echo "1!!$content";
    }else if($bytesRead > $fileSize){
        //File edit or delete detected, whole file read again.
        $content = file_get_contents($fileURL);
        file_put_contents("bytes.txt", strlen($content));
        echo "2!!$content";
    }else if($bytesRead == $fileSize){
        //No new data found, no action taken.
        echo "0!!";
    }
}else{
    //File delete detected, reading whole file when available
    echo "-1!!";
    file_put_contents("bytes.txt", "0");
}
?>

It works perfect when I run it and does what is expected. When I edit the file from the same PC and my server it works instantly and returns the correct values. However when I edit the file from another PC, my script takes about 4-6 seconds to read the correct filesize of the file.

I added clearstatcache(); on top of my script, because I think its a caching issue. But the strange thing is that when I change the file from the server PC it responds instantly, but from another it doesn't.

On top of that as soon as the other PC changes the file, I see the file change in Windows with the filesize and content but for some reason, it takes Apache about 4-6 seconds to detect the change. In those 4-6 seconds it receives the old filesize from before the change.

So I have the following questions:

  1. Is the filesize information cached anywhere maybe either on the Apache server or inside Windows?
  2. If question 1 applies, is there anyway to remove or disable this caching?
  3. Is it possible this isnt a caching problem?
1 Answers

I think that in Your local PC php has development settings.

So I suggest to check php.ini for this param: realpath_cache_ttl

Which is:

realpath_cache_ttl integer

Duration of time (in seconds) for which to cache realpath information for a given file or directory. For systems with rarely changing files, consider increasing the value.

to test it, php info both locally and on server to check that value:

<?php phpinfo();
Related