[web] Displaying the 10 most recently uploaded photos..

Started by
7 comments, last by capn_midnight 16 years, 3 months ago
I have this thing called a 'netbot', its a camera that uploads photos to a webserver every so often. The netbot is pretty dumb, it cant really be configured, it just uploads photos (giving them a timestamp for a name). What I want to do is setup a webpage that displays the 10 most recent photos. Of course since I dont know the names of the photos (they are ever changing), I need some way to do a query to find out 'what are the names of the 10 most recent jpgs in directory X'. Then I need a way to use that list/info to display thumbnails for those 10 photos. I have some limited html/&#106avascript experience, but no idea how to do this. Thanks for any help!
Advertisement
A client-side approach has no means of determining the contents of a server-side directory. You need to have control over the server to do this. A server-side script could certainly do this, though (for instance, PHP has glob and filemtime).
Yeah I figured it couldnt be done with html/&#106avascript alone. Unfortunately I have very little experience with php, can anyone show a simple example for how I would write a php program that generates a 'list' of the 10 most recent pictures in a directory? Then of course I need a way to 'pass' this information to my html so the images can be displayed correct?
The opendir manual page has an example of reading a list of files (and their file types) from a directory. ToohrVyk already linked to the page to show how to get file modification dates.

Here's a quick example PHP file...
<html><body><?PHP$files = array();$dir = "./camera";if (is_dir($dir)) {    if ($dh = opendir($dir)) {        while (($file = readdir($dh)) !== false) {            //TODO - should also check that $file ends with ".jpeg" etc...            if( is_file($file) )                $files[] = $file;        }        closedir($dh);    }}//TODO - sort $files by modification date using filemtime...$i = 0;foreach( $files as $file ){    echo '<img src="'.$dir.'/'.$file.'"/>';    $i = $i + 1;    if( $i >= 10 )        break;}?></html></body>
Ok great, everything looks really simple. However I think there is a problem with my webserver.. im going through this simple tutorial, but I cant get their example to work. Check out my test php...

http://wieland.mystarband.net/php.php

Shouldnt that be printing the Neo: line?
The above code can be shortened as:

foreach (glob("*.jpeg") as $file)  $files[$file] = filemtime($file);arsort($files);array_splice($files,10);foreach ($files as $file => $time)  printf ('<img src="./%s" />\n', $file);


Also, your web host does not seem to support PHP.
Yeah were gonna move the moose cam to another web server (were capturing pictures of mooses incase anyone was curious).

So it looks like those few lines of code do EXACTLY what I need. But if you dont mind id like to understand what each line is doing...

Quote:
foreach (glob("*.jpeg") as $file)
$files[$file] = filemtime($file);


What does glob stand for? Also I assume if the pictures were in a sub directory could I simply say "somedir\*.jpeg"?

Quote:
$files[$file] = filemtime($file);


What is the filetime() function doing exactly?

Quote:
arsort($files);
array_splice($files,10);


The sorting takes place here?
Quote:Original post by ZealousEngine
Quote:
foreach (glob("*.jpeg") as $file)
$files[$file] = filemtime($file);


What does glob stand for? Also I assume if the pictures were in a sub directory could I simply say "somedir\*.jpeg"?

I'm not sure what it stands for, but here's some background info on glob.
Glob uses Regular expression syntax (where '\' is a special character, as are '*' and '.'), so you'd probably need to write: glob("somedir\\.*\.jpeg")

Quote:Original post by ZealousEngine
Quote:
$files[$file] = filemtime($file);

What is the filetime() function doing exactly?

filemtime is getting the modification date for $file.
Also, $files is being used as an associative array here, so that line is creating an association with the value of '$file' as the association's 'key' and the value of 'filemtime($file)' as the association's 'value' and storing that association inside '$files'.

Quote:Original post by ZealousEngine
Quote:
arsort($files);
array_splice($files,10);

The sorting takes place here?

Yep, arsort sorts the 'values' contained in $files, and importantly it maintains the associations between each value and it's key (Remember that when the associations were created earlier, the 'value' was the modification date, and the key was the file-name).
array_splice is then used to throw away everything except the first 10 entries.

The foreach syntax then goes through each association contained in '$files' and stores the 'key' in '$file' and the 'value' in '$time'. As you can see, '$time' isn't actually being used, but in order to write a foreach like this in PHP you have to put it there anyway.
Once you have your list of files, as long as your filenames are a timestamp format that runs units from largest to smallest (i.e. years then months then days then hours then minutes then seconds) then you wouldn't even have to parse the timestamp, you could just sort the list lexicographically.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement