Display latest image in directory ending in

后端 未结 2 355
情深已故
情深已故 2021-01-28 12:11

I\'d like to display the last modified image in a directory that ends in a specific way.

I have it working to display the last file in the directory I only don\'t know h

相关标签:
2条回答
  • 2021-01-28 12:48

    This should sort it out for you, basically just add another rule to filter in images ending in your required field.

    Basically just use the strpos() function to determine if the value you are looking for is in the file name.

    Also wrapped the code within a function, allowing bot the desired folder location, and desired string which the file must end in.

    <?php
    function getLatestImage($folderName, $imageEnding) {
        $newest_mtime = 0;
        $base_url = 'images/folio/'.$folderName;        
        $file_ending = $imageEnding;
        $show_file = 'images/folio/no-image.jpg';
        if ($handle = opendir($base_url)) {
            while (false !== ($latestFile = readdir($handle))) {
                if (($latestFile != '.') && ($latestFile != '..') && ($latestFile != '.htaccess') && (strpos($latestFile, $file_ending))) {
                 $mtime = filemtime("$base_url/$latestFile");
                    if ($mtime > $newest_mtime) {
                        $newest_mtime = $mtime;
                        $show_file = "$base_url/$latestFile";
                    }
                }
            }
        }
        return $show_file;
    }
    
    echo '<img src="' .getLatestImage('foldername', 'file_ending.jpg'). '" alt="Latest from the web">';
    ?>
    
    0 讨论(0)
  • 2021-01-28 12:58

    That is my way, resolving this issue, I am not a programmer, it is try on error product :)

    I used it in a wordpress installation and had to install the plugin Allow PHP in Posts and Pages (thats why the php tags are different, normally instead of [PHP] [/PHP])


    [PHP]
    global $path;    // path to directory has to be global, using it later in html
    
    $dir = "Planetwebcam/";
    
    $files = scandir($dir, 1);                // read directory backkwards
    
    $picture = ($files[0]);                   // get las picture file name
    
    $path = "http://www.yourdomain.ch/Planetwebcam/$picture";
    
    [/PHP]
    
    
    <a href="[PHP]global $path;echo $path; [/PHP]">
    <img class="alignnone size-large" alt="Last picture" src="[PHP]global 
    $path;echo $path; [/PHP]" width="1600" height="1200" /></a>
    
    0 讨论(0)
提交回复
热议问题