E
E
Evgenya-k22019-06-17 17:44:45
Yii
Evgenya-k2, 2019-06-17 17:44:45

How to implement automatic page refresh on file change?

Good afternoon!
On the server in the directory frontend/web/files/is the test.json file. The file is automatically updated ~ 1 time per half hour.
The controller reads the file data for the report:

public function actionReport(){
        $file = file_get_contents('http://site.ru/files/test.json');
        
        for ($i = 0; $i <= 31; ++$i) { 
            $file = str_replace(chr($i), "", $file); 
        }
        $file = str_replace(chr(127), "", $file);

        if (0 === strpos(bin2hex($file), 'efbbbf')) {
        $file = substr($file, 3);
        }

        $content = json_decode($file, true);

        return $this->render('report', [
            'content' => $content,
        ]);
    }

The resulting data is then displayed in a view.
How can you track file changes, and, accordingly, automatically update the page when it changes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav, 2019-06-17
@Evgenya-k2

ajax + https://php.ru/manual/function.filemtime.html
or
websocket + https://php.ru/manual/function.filemtime.html

M
Moses Fender, 2019-06-17
@mosesfender

I strongly recommend checking not by the contents of the file, but by the time it was changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question