Rotating image, File Paser, and Expiration

ok, I’ve been told what I would like to do has to be done in php or something similar. I don’t know much on writing code or php but here is what I am trying todo here.

I want to create a directory and upload images in that directory with file names like:

10202012.jpg
10272012.jpg
11152012.jpg
12052012.jpg

What I would like to do is have the script search the directory and find the newest date, in this case is 10202012.jpg and compare that date to todays date of 10132012 and then display this image until 11:00 pm of the date of the filename, then expire and display the next image wich is 10272012.jpg and do this all over for all the files in that directory.

Thanks

10202012 would be the oldest date, not the newest. At any rate though, due to the way filesystems sort files, it would be better to name the files in YYYYMMDD format … IE: 20121020, 20121027, etc.

Then you could do something like:


<?php
/**
Example file list:
.
..
20121020.jpg
20121027.jpg
20121105.jpg
*/

$curdate = date('Ymd');
$curtime = date('Hi');

$path = '/path/to/files'; // A relative path would be better
$filelist = scandir($path);

$img = ''; // You can also initialize this as an int to set a 'default' image if one for the curdate isn't found

foreach ($filelist as $key => $file)
{
    if ( (!is_dir($path . '/' . $file)) && (substr($file, 0, 8) == $curdate) ) {
        if ( ($curtime < 2300) || (!array_key_exists($key + 1, $filelist)) ) {
            $img = $key;
            break;
        } else {
            $img = $key + 1;
            break;
        }
    }
}

$curimage = $path . '/' . $filelist[$img];

echo "<img src='{$curimage}' />";

?>

Thanks for the help, but I’m not sure what I am doing wrong, but I can’t seem to get this to work.

Is it really 11 PM you’re wanting, and not midnight?

As Keith suggested, put your files in YYYYMMDD format - not necessarily because of how the OS stores them (which can be variable), but because you can compare them in PHP (which is what scandir does).


$files = scandir($dir); //Get files. They will be in order.
while ($files[0] == "." || $files[0] == "..") { array_shift($files); } //Get rid of directories.
$files = array_filter($files,function($file) { return ((int)$file >= ((date('G') < 23) ? (int)date('Ymd') : (int)date('Ymd',strotime('tomorrow')))); }); //Get rid of any files that are too old.
$imgout = array_shift($files);

EDIT: Bit shorter just to shift the array again.

You will see better performance if you store all files you have in a DB as you manipulate your directory. You’d then run a query against your DB of images and use the returned record set to present your data / files.

Thanks, works great. Now I just need to figure out a work around for the server time and local time.