SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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($file08) == $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}' />";

    ?>
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, but I'm not sure what I am doing wrong, but I can't seem to get this to work.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    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).
    PHP Code:
    $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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,095
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, works great. Now I just need to figure out a work around for the server time and local time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •