WXforum.net
Web Weather => Weather Website PHP/AJAX scripting => Topic started by: kplew on January 11, 2019, 03:11:07 PM
-
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
-
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:
echo "<option value=\"wxvideodisp.php?src=$page\">".$filename."</option>";
-
Thanks I'll give it a try.
-
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:
<?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>
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 ;)
-
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
-
... but if I manually update options.txt it works great.
If you really mean manually then the script doesn't work at all :roll:
I may have something wrong or yet again fell foul of php7 ](*,)
Wait a min ... couple of silly errors, which would have only (last) one option being written to file.
If nothing was being written to file then I'll have to wait 'til I get my PC back and start checking things for real instead of abstractly #-o
<?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);
$option_list = "";
foreach($files as $page){
$filename = basename($page,".mp4");
$option_list .= "<option value = 'wxvideodisp.php?src=" .$page. "'>" .$filename. "</option>\n";
}
file_put_contents('options.txt', $option_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>
.. OR try this as a standalone test.php
<?php
$files = glob("videos/*.mp4");
$files = array_combine(array_map("filemtime", $files), $files);
krsort($files);
$option_list = "";
foreach($files as $page){
$filename = basename($page,".mp4");
$option_list .= "<option value = 'wxvideodisp.php?src=" .$page. "'>" .$filename. "</option>\n";
}
file_put_contents('testing.txt', $options_list);
// use old style to see if it got done
$fh = fopen('testing.txt','r');
while ($line = fgets($fh)) {
echo($line);
}
fclose($fh);
?>
EDIT: was trying to write options_list instead of option_list to file !!
-
We are accessing options.txt (every time the script runs the filetime is updated) but we aren't writing anything to the file.
Thanks
-
Silly beteljuice !!!
Code in above post corrected ..
EDIT: was trying to write options_list instead of option_list to file !!
:oops: ](*,)
-
Works perfect. Thanks. :grin:
-
When / If the beteljuice gets his old workhorse back - "Have I got some code for you" :shock:
-
Well.... ?doit=1 doesn't force an update. The variable doesn't get set.
Thanks
-
I was able to get it working by adding $_Get to the routine. Thanks for all your help.
<?php
$doit = $_GET['doit']; // Gets ?name=value
$time_to_die = time() - (60 * 60 * 25); // 25hrs
if(isset($doit) || (filemtime('options.txt') < $time_to_die)) {
$files = glob("videos/*.mp4");
$files = array_combine(array_map("filemtime", $files), $files);
krsort($files);
$option_list = "";
foreach($files as $page){
$filename = basename($page,".mp4");
$option_list .= "<option value = 'wxvideodisp.php?src=" .$page. "'>" .$filename. "</option>\n";
}
file_put_contents('options.txt', $option_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>
-
the beteljuice is guilty of old school php where many, many short-cuts were not only tolerated but considered good practice.
I was going to try:
if(isset($_REQUEST('doit')) || (filemtime('options.txt') < $time_to_die)) {
When I got my PC back, but since I last typed the beteljuice experience has gone from ailing to bl**dy - managed to set my computer room afire :shock:
Spent all day trying to clean soot off things and feeling I must have offended some deity or other - not a good week !
PS. Did you get my PM ? (reply by PM please)
-
Sorry for your troubles- you have helped me immensely these past few days so all is not lost!!