Author Topic: glob  (Read 574 times)

0 Members and 1 Guest are viewing this topic.

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
glob
« 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

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 482
    • LexMAWeather
Re: glob
« Reply #1 on: January 11, 2019, 05:01:20 PM »
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: [Select]
echo "<option value=\"wxvideodisp.php?src=$page\">".$filename."</option>";

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #2 on: January 11, 2019, 05:48:46 PM »
Thanks I'll give it a try.

Online the beteljuice

  • the beteljuice
  • Contributor
  • ***
  • Posts: 129
    • test site
Re: glob
« Reply #3 on: January 14, 2019, 06:04:00 PM »
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: [Select]
<?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  ;)
Imagine what you will KNOW tomorrow !

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #4 on: January 14, 2019, 07:58:29 PM »
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

Online the beteljuice

  • the beteljuice
  • Contributor
  • ***
  • Posts: 129
    • test site
Re: glob
« Reply #5 on: January 14, 2019, 09:57:33 PM »
... 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
Code: [Select]
<?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
Code: [Select]
<?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 !!
« Last Edit: January 15, 2019, 11:21:39 PM by beteljuice »
Imagine what you will KNOW tomorrow !

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #6 on: January 15, 2019, 07:15:21 AM »
We are accessing options.txt (every time the script runs the filetime is updated) but we aren't writing anything to the file.

Thanks

Online the beteljuice

  • the beteljuice
  • Contributor
  • ***
  • Posts: 129
    • test site
Re: glob
« Reply #7 on: January 15, 2019, 11:23:51 PM »
Silly beteljuice !!!

Code in above post corrected ..
Quote
EDIT: was trying to write options_list instead of option_list to file !!
:oops:  ](*,)
Imagine what you will KNOW tomorrow !

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #8 on: January 16, 2019, 07:12:23 AM »
Works perfect. Thanks. :grin:

Online the beteljuice

  • the beteljuice
  • Contributor
  • ***
  • Posts: 129
    • test site
Re: glob
« Reply #9 on: January 16, 2019, 12:00:09 PM »
When / If the beteljuice gets his old workhorse back - "Have I got some code for you"  :shock:
Imagine what you will KNOW tomorrow !

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #10 on: January 16, 2019, 05:28:07 PM »
Well.... ?doit=1 doesn't force an update. The variable doesn't get set.

Thanks

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #11 on: January 17, 2019, 08:10:43 AM »
I was able to get it working by adding $_Get to the routine. Thanks for all your help.

Code: [Select]
<?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>

Online the beteljuice

  • the beteljuice
  • Contributor
  • ***
  • Posts: 129
    • test site
Re: glob
« Reply #12 on: January 17, 2019, 10:17:51 AM »
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:
Code: [Select]
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)
« Last Edit: January 17, 2019, 10:23:11 AM by beteljuice »
Imagine what you will KNOW tomorrow !

Offline kplew

  • Member
  • *
  • Posts: 14
    • Plew Farm Weather
Re: glob
« Reply #13 on: January 17, 2019, 10:23:46 AM »
Sorry for your troubles- you have helped me immensely these past few days so all is not lost!!

 

anything