如何在修改.txt文件的内容时刷新我的.php站点

I have a .php file that displays a .txt from my FTP server to a webpage.

My problem is that I want to get the .php page to refresh when something is added to the .txt file.

Right now I'm using this:

<?php
    header("Refresh: 5; URL=$url1");
    include('filename.txt');
?>

Which refreshes the page every five seconds to see if the .txt file is modified. I dislike this method because it spams my logs of who is viewing the webpage with the same information.

I was wondering if I could modify the .php to refresh only filename.txt is modified.

Use filetime() for this. http://php.net/manual/en/function.filemtime.php

Example from there

<?php
// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.",  filemtime($filename));
}

You can use a logic combination of PHP and Javascript (more specifically JQuery) with a trick. Of course this is a work-around approach (can be modified to make it better).

Pseudo-example can be like:

// A new PHP file "proxy.php"

<?php
        if (!empty($_GET) && !empty($_GET['check'])) {
            $previouslyChecked = $_GET['check'];

            if (filemtime("filename.txt") > $previouslyChecked) {
               echo 1;
            } else {
               echo 0;
            }
            die();
        }

// Your PHP File

<html>
<head>
    <script type="text/javascript" src="jquery.min.js"></script>
</head>

<body>
<?php
    include('filename.txt');
    $lastModified = filemtime("filename.txt");
?>
    <input type="hidden" id="loadedAt" value="<?php echo $lastModified; ?>"/>


     <script type="text/javascript">

        function reloadPage(){
           console.log("within reload");
           window.location.reload();
        }

        function checkFile(){
            console.log("checkfile");
            jQuery.ajax({
                type: "GET",
                url: "proxy.php",
                data: {check: jQuery("#loadedAt").val()},
                success: function(data){ 
                    if (data == 1) {    
                        console.log("reload called");
                        reloadPage(); 
                    }
                    setTimeout(checkFile, 5000);
                }
            });
        };

        jQuery(document).ready(function(){
            console.log("checkfile called");
            checkFile();
        });
    </script>

</body>
</html>

Hope this may work.