0

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.

miguelbgouveia
  • 2,861
  • 5
  • 24
  • 44
raw
  • 1
  • 1
  • [filemtime](http://php.net/manual/en/function.filemtime.php) is handy for checking when a file was last modified... – Henders Mar 31 '16 at 09:20
  • This might be a job for web sockets. – Wayne Whitty Mar 31 '16 at 09:28
  • If you can use javascript, you also can get file content in AJAX (Unfortunately, every some seconds) and check if it changes to refresh at this moment. – DiD Mar 31 '16 at 09:36
  • 1
    This is related to server pushing function, this [post](http://stackoverflow.com/questions/19995/is-there-some-way-to-push-data-from-web-server-to-browser) maybe help. – panda Mar 31 '16 at 09:38

2 Answers2

0

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));
}
E. Gimenez
  • 108
  • 5
0

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.

Himel Nag Rana
  • 724
  • 1
  • 13
  • 18
  • 1
    Didn't work for me, it doesn't refresh at all anymore. – raw Mar 31 '16 at 22:26
  • I actually stripped my actual code to only the related parts. Obviously copy pasting this will not work as there is no perfect HTML format mentioned here. Edited my code. Please take a look. – Himel Nag Rana Apr 04 '16 at 05:07