0

I wrote a code to re-read a content of a file (on the server) every time the file is modified. The scenario is like this:

- The webpage is loaded
- If the file (on the server) is newer than the starting time of the page (the time when the webpage was started), the content of the file is read
- If the file is modified later, the content must be read again by PHP script

I tried this using EventSource. Here is the code for the browser:

<html>
<head>

<?php
$startTime = time();
$flag = 0;
?>

<script type="text/javascript" language="javascript">
lastFileTime = <?php echo $startTime; ?>;
var fileTime;

if(typeof(EventSource) !== "undefined") {
    var source=new EventSource("getFileTime.php");
    source.onmessage = function(event) {
        fileTime = parseInt(event.data);
        if (fileTime > lastFileTime) {
            readFile();
            lastFileTime = fileTime;
        }
    };
}
else {
    alert("Sorry, your browser does not support server-sent events.");
}

function readFile() {
    <?php
    $fid = fopen("file.bin", "rb");

    ...      // Read the content of the file

    $flag = $flag + 1;
    ?>

    ...      // Transfer the content of the file to JavaScript variables

    flag = <?php echo $flag; ?>;
}

</script>
</head>

<body>
...
</body>
</html>

And here is the server-side code (getFileTime.php):

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$filetime = filemtime("file.bin");
echo "data: {$filetime}\n\n";
flush();
?>

When I start the webpage and created file.bin afterwards, readFile() is called for the first time (I checked the value flag = 1. But then, if I modified file.bin again, obviously readFile() is not called. I checked the content of the file; it's still from the previous file, and also flag is still 1. It seems that a PHP script in a JavaScript function can only be called once. How to re-execute the PHP script in a JavaScript function?

1 Answer 1

2

Your PHP script needs to remain active, sending new events to the client when something changes:

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$lastFiletime = null;

while (true) {
  $filetime = filemtime("file.bin");

  if ($filetime != $lastFiletime) {
    echo "data: {$filetime}\n\n";
    $lastFiletime = $filetime;
    flush();
  }
}
2
  • What I need is to re-execute the PHP script in readFile() every time file.bin is modified. I have no problem with the script in getFileTime.php; it works fine. Thanks anyway, your comment has given me an idea about how to do this another way. Commented Jan 3, 2014 at 3:48
  • That's not how that works, you cannot do it that way. Something has to remain active to watch for changes to the file, and that something might as well be PHP, since it's PHP that's sending events to the client. You cannot fully re-execute the PHP script when the file changes; that's backwards. You can only monitor the file for change from a running program. Commented Jan 3, 2014 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.