0

I have folder with dynamic files, every 2 seconds there may be a new file. I want check every 2 seconds if there are any new files or not. If there are new files, add them to array.

Every file size about 1m.

I get files then start reading -- file after file. I need to update array when reading files and continue reading and updating until the browser is closed.

I use this code:

if ($files = glob('/files/'.$video_id.'*.ts')) {
    $files = array_slice($files, -6, 6, true);
    foreach ($files as &$file) {
        if (!file_exists($file)) {
            unset($files[$file]);
            asort($files);
            reset($files);
        }
        $fp = @fopen($file, "r");
        echo fread($fp, filesize($file));
        $newfiles = glob('/files/'.video_id.'*.ts');
        $newfiles = array_slice($newfiles, -6, 6, true);
        foreach ($newfiles as &$newfile) {
            if (!in_array($newfile, $files, true)) {
                array_push($files, $newfile);
                asort($files);
                reset($files);
            }
        }
    }
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
espooo
  • 17
  • 5

1 Answers1

0

first you have to understand that php strings cannot be refreshed inside php itself when the string stored in Server memory .in some cases you can use in the top of your php script :

header('Refresh: 2'); 

but ! that's gonna refresh your entire page even if you include it from other file. or you put it in a specific place. header() is made to reload or redirect the entire page.

☼ Solution : to avoid refreshing the entire page use ( Ajax ).
Example :

Example image



✔ -   to call your (php script) using (Ajax) :
        ......................................................................................................................

  • 1 - creat a New php File inside your script directory
         and name it Example : ( body.php ) then add your (php code you want to reload) inside it

  • 2 - now in you index page add this :

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    

then the simplest way to load the page we just created (body.php) is (JQuery load):

    <script type="text/javascript">
      setInterval(function(){ 
           $('#Request').load('Request.php'); 
      },  2000);

    </script>

then add this to your html area (where you want your code to show up) :

    <div id="Request"> your html video container here </div>

read this article for more in-depth : https://en.wikipedia.org/wiki/Ajax_(programming)

i saw that you trying to call some video links so change :
       > your html video container here < to feet your project.
        the important thing that you understand the general idea .
other ways we can't do your whole home-work for you.
the few things that's need to be changed to feet your script is your job .

Note :   the "2000" value we used in (javascript) is time by millisecond which = 2 sec you can change it next time if you want to add more time or less time . and remember ( 1000= 1 sec ) and base on that do the rest math by yourself , enjoy.

The Doctor
  • 636
  • 1
  • 7
  • 23