简体   繁体   中英

Display latest image from directory

I use this to display the latest image from 2 directories where images from cameras upload every 10s The code works, but as I know I might end up with potentially 10s of thousands of images in each directory I believe the code is not optimized for the situation. Also I reload the whole page every 10s where maybe it would be more efficient to just update the images. Could someone help giving me directions to optimize this? Thanks a lot.

<?php
    $page = $_SERVER['PHP_SELF'];
    $sec = "10";
    $base_url_east = 'East/snap/';
    $base_url_south = 'South/snap/';
    $newest_mtime_east = 0;
    $show_file_east = 'BROKEN';
    if ($handle = opendir($base_url_east)) {
        while (false !== ($file = readdir($handle))) {
            if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
                $mtime = filemtime("$base_url_east/$file");
                if ($mtime > $newest_mtime_east) {
                    $newest_mtime_east = $mtime;
                    $show_file_east = "$base_url_east/$file";
                }
            }
        }
    }
    $newest_mtime_south = 0;
    $show_file_south = 'BROKEN';
    if ($handle = opendir($base_url_south)) {
        while (false !== ($file = readdir($handle))) {
            if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
                $mtime = filemtime("$base_url_south/$file");
                if ($mtime > $newest_mtime_south) {
                    $newest_mtime_south = $mtime;
                    $show_file_south = "$base_url_south/$file";
                }
            }
        }
    }
?>
<html>
    <head>
        <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
    </head>
    <body bgcolor="#000000">
        <center>
        <?php
            print '<img src="' .$show_file_east. '" alt="Latest image uploaded" width="720" height="480">';
            print '<img src="' .$show_file_south. '" alt="Latest image uploaded" width="720" height="480">';
        ?>
        </center>
    </body>
</html>

If the new files are named East/snap/current.jpg and South/snap/current.jpg , you could easily use the HTML even without PHP like this

<img src="East/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">
<img src="South/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">

The one/script uploads the photos should be responsible for

  • copying the already uploaded current.jpg to 2016-08-13-07:00:00.jpg (or some other filename containing eg. the date
  • copying the new file to current.jpg

EDIT

If you cannot modify the filenames like I said above, try

$string = date('Ymd-H'); // 20160812-12
$files = glob('Schedule_' . $string . '*.jpg');

This will get you all files into an array, which were taken in the current hour. It could be that you get a problem, if the script is called 9 seconds, after the hour changed, because it won't find an image from that hour. But with this way you can minimize the count of scanned files.

EDIT

This script is not tested, but should return one recent file for one folder:

<?php

$basePath = '/home/user/camera/East/snap';

// Scans files from the current and the last minutes
// this ensures that always files will be found, even if the minute changed
$date = new \DateTime();
$date->setTimeZone(new \DateTimeZone('America/Vancouver'));

$prefixCurrentMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$date->sub(new \DateInterval('PT1M'));
$prefixLastMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';

$files = array_merge(
    glob($prefixLastMinute),
    glob($prefixCurrentMinute)
);

$lastFile = 'dummy.jpg';

if (is_array($files) AND count($files) > 0) {
    // this methods sorts the files "regularly" by default, and I guess
    // regularly means alpha-numeric in ascending order ...
    sort($files);

    $lastFile = $files[0];
    // use this, if the order is not ascending after using sort() on the array
    // $lastFile = $files[count($files) - 1];
}

$eastPhoto = basename($lastFile);
?>

<img src="East/snap/<?php echo $eastPhoto; ?>" alt="Latest image uploaded" width="720" height="480">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM