显示目录

时间:2016-08-13 12:06:54

标签: php

我使用它来显示来自2个目录的最新图像,其中来自摄像机的图像每10秒上传一次 代码可以工作,但据我所知,我可能最终会在每个目录中有数千个图像,我相信代码并没有针对这种情况进行优化。 此外,我每10秒重新加载整个页面,可能只是更新图像更有效。 有人可以帮我指点优化吗? 非常感谢。

<?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>

1 个答案:

答案 0 :(得分:0)

如果新文件名为 East / snap / current.jpg South / snap / current.jpg ,即使没有像这样的PHP,您也可以轻松使用HTML

<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">

一个/脚本上传的照片应该负责

  • 将已上传的 current.jpg 复制到 2016-08-13-07:00:00.jpg (或其他包含例如日期的文件名
  • 将新文件复制到 current.jpg

修改

如果您无法修改上述文件名,请尝试

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

这会将所有文件放入一个数组中,这些文件是在当前时间内拍摄的。可能是你遇到了问题,如果在小时改变后9秒钟调用脚本,因为它无法从那个小时找到图像。但通过这种方式,您可以最大限度地减少扫描文件的数量。

修改

此脚本未经过测试,但应返回一个文件夹的最新文件:

<?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">