Determine if a txt file is done being updated/written to?

I have an EXE app on my home server that uploads a .txt file (live lightning strikes) every 1 minute to my web server. Then I use this PHP code to read the text file and load it into their software (like Google Earth) where it displays it visually on the map. I’m using PHP to hide public view of strikes(us).txt file as they will scrape the data.

My issue is, sometimes the software calls the PHP file which reads the .txt file and it’s still being written to, not fully complete, which fails in the software. How can I use PHP to determine if this file is not open, being written to, or incomplete and either wait 2 seconds and try again or something alike. Thank you for any help.

<?php
header('Content-Type: text/plain');

if (preg_match('/(GRLevel2|GRLevel3|GR2Analyst)/', $_SERVER['HTTP_USER_AGENT'])) {
echo file_get_contents( "Strikes(US).txt" );
} else {
echo 'Please Copy & Paste the URL directly into your GRLevelX Placefile Manager!';
exit;
}

Let’s try to add a loop that checks the file size every few seconds and proceed only if the size hasn’t changed within the loop. This is not a foolproof method, as it assumes that the file size will change when it’s being written, and it may introduce a delay in processing, but it could solve your problem in most cases.

Here’s a modified version of your code that checks the file size in a loop:

<?php
header('Content-Type: text/plain');

if (preg_match('/(GRLevel2|GRLevel3|GR2Analyst)/', $_SERVER['HTTP_USER_AGENT'])) {
    $filePath = "Strikes(US).txt";
    $tries = 5; // Number of tries
    $waitSeconds = 2; // Time to wait between each try

    for ($i = 0; $i < $tries; $i++) {
        clearstatcache(); // Clear the file status cache
        $sizeBefore = filesize($filePath);
        sleep($waitSeconds);
        clearstatcache(); // Clear the file status cache again
        $sizeAfter = filesize($filePath);

        if ($sizeBefore == $sizeAfter) {
            echo file_get_contents($filePath);
            break;
        }
        
        // If this is the last loop iteration and the file is still changing, handle the error as needed
        if ($i == $tries - 1) {
            echo "File is being updated. Please try again later.";
        }
    }
} else {
    echo 'Please Copy & Paste the URL directly into your GRLevelX Placefile Manager!';
    exit;
}
?>

This code will attempt to read the file up to 5 times (as defined by $tries), waiting 2 seconds between each attempt (as defined by $waitSeconds). If the file size hasn’t changed within that time, the file contents will be echoed out. If the loop finishes and the file size is still changing, an error message will be echoed.

Please note that the clearstatcache() function is used to clear PHP’s internal cache of information about the file. Without this, PHP might return cached information about the file size, which could cause this loop to exit prematurely.

Also, note that this method does not actually check if the file is being written to; it only checks if the size has changed. If the file is being written in a way that doesn’t change the size, or if the writes are happening faster than the loop’s delay, this method might not work. It’s a workaround that might work for your specific use case, but it’s worth testing thoroughly to make sure it meets your needs. Good luck, if you have not yet got the solution, use this

Sponsor our Newsletter | Privacy Policy | Terms of Service