Web Weather > Weather Website PHP/AJAX scripting

glob

(1/3) > >>

kplew:
A little advice from the php experts please...... I'm using glob to display a list of daily weather videos from a directory on my server. The video is updated daily and archived videos are available so I've been using glob to create the array that populates the drop down box. It works but has become slow due to the number of files in the directory. This is what I'm doing now:

<form>
<select name="s1" onChange="window.open(this.options[this.selectedIndex].value,'_blank')">
      <option value="wxwebcamvid.php" selected="selected">Select a date</option>
  <?php
       $files = glob("videos/*.mp4");
       $files = array_combine(array_map("filemtime", $files), $files);
       krsort($files);
       
       foreach($files as $page){
       $filename = basename($page,".mp4");
       
       echo "<option value=wxvideodisp.php?src=$page>".$filename."</option>";
  }
?>

</select>
</form>

Any suggestions to speed it up?
The page is here: https://www.plewfarm.com/weather/wxwebcamvid.php

Thanks

Jasiu:
If you haven't already, you might first want to use microtime() to time the glob() call or any other pieces of code you think might be chewing up CPU. I recommend putting the results into HTML comments and then using view-source to have a look.

One bug fix: You need double-quotes around the value text:


--- Code: ---echo "<option value=\"wxvideodisp.php?src=$page\">".$filename."</option>";
--- End code ---

kplew:
Thanks I'll give it a try.

the beteljuice:
You appear to be the victim of what should be elegant programming !

It is of course all geared to how you want form your o/p.

Another part is looking up all the filemtime, combining an array then 'key' sorting.
filemtime is unreliable in that if you ever have edit / move / reupload your files everything will go a bit sick.

Regardless of all that, at some point you will need some sort of 'house-keeping' facility as the directory content gets larger and larger.

Short term at least the beteljuice suggests writing the options to a file.

the beteljuices main (XP) machine is at the vets getting life support at the moment - so totally untested and off the top of my head - something like:

--- Code: ---<?php

$time_to_die = time() - (60 * 60 * 30); // 30hrs
if(isset($doit) || (filemtime('options.txt') < $time_to_die)) {
$files = glob("videos/*.mp4");
    $files = array_combine(array_map("filemtime", $files), $files);
    krsort($files);
       
    foreach($files as $page){
       $filename = basename($page,".mp4");
   $option_list = "<option value = 'wxvideodisp.php?src=" .$page. "'>" .$filename. "</option>\n";
}

file_put_contents('options.txt', $options_list);
}

?>

<form>
<select name="s1" onChange="window.open(this.options[this.selectedIndex].value,'_blank')">
<option value="wxwebcamvid.php" selected="selected">Select a date</option>
<?php include "options.txt"; ?>

</select>
</form>

--- End code ---
To avoid errors you must initially create an empty file called options.txt on your server.
Then the first time you 'call' your page add ?doit=1 to the url.
That should create populate the include file that creates the drop down list - this is also the routine you would do every day after you've done a new archive file.
If you forget, then if the options.txt file is older than 30 hours is will be rebuilt (while the viewer has to wait)

Totally untested, may be silly errors so create a test page first !!!!!

If that all works, the next thing will be the 'picking' method - the beteljuice has a thought or two on that  ;)

kplew:
I actually started down the same road but...

So bad new good news

The script doesn't populate options.txt with ?doit=1, but if I manually update options.txt it works great.

thanks

Navigation

[0] Message Index

[#] Next page

Go to full version