Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Display only last image in directory

    I have images in a directory that are uploaded from smart phones. When uploaded the file name is appended with date & time stamp in Epoch format
    (e.g. image-1392938655.jpg).

    I want to display, on a specific page, only the latest image in that directory.

    Can anyone give me the code I need?

    Thank You

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,519
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    var ImageArray=['4392939655.jpg','5392938656.jpg','3392938655.jpg']
    
    ImageArray.sort(function(a,b){ return b.split('.')[0]-a.split('.')[0]; });
    alert(ImageArray[0]);
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    can you view the file names by browsing to the folder? in other words, does it provide a directory listing?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    New Coder
    Join Date
    Mar 2011
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    No, if I browse to the URL of the directory that contains the images "Access is Forbidden.
    Browsing to the URL of any image will display that image.

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I tried your code and I get a popup window that displays one of the file name in your image array (5392938656.jpg).
    Where do I specify the directory locations of my images?
    Thanks

  • #6
    New Coder
    Join Date
    Mar 2011
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I've made some progress since last post.

    I have this code and it does what I want.

    It displays only the last image in directory where I want it on my home page.
    Only problem so far is that image is displayed in it's original size.
    I need it to be resized to 250px wide while maintaining height ratio.
    I've tried to do it myself but it is beyond my capabilities.
    Any suggestions?



    <?php
    $dir = '../images/catches/test/';
    $base_url = 'http://pellsonline.com/images/catches/test/';
    $newest_mtime = 0;
    $show_file = 'BROKEN';
    if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
    if (($file != '.') && ($file != '..')) {
    $mtime = filemtime("$dir/$file");
    if ($mtime > $newest_mtime) {
    $newest_mtime = $mtime;
    $show_file = "$base_url/$file";
    }
    }
    }
    }
    print '<img src="' .$show_file. '" alt="Image Title Here">';
    ?>


    If you try it the page is:
    pellsonline.com/new

    FYI
    The image that displays was resized before uploading.
    Last edited by laurenor; 03-20-2014 at 05:08 PM.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    did you try css (you might want to give the image tag an id):
    Code:
    img{height:auto;width:250px;}


  •  

    Posting Permissions

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