PHP:从数组中删除旧图像并显示默认图像

时间:2019-02-19 16:50:55

标签: php

我有一个网站,用于显示已定义文件夹中最近7天的网络摄像头图像。

我们为此编写了一个代码,该代码查找特定的文件类型,删除定义期限以上的任何文件,然后将其与创建日期一起放入数组。此数组用于按日期顺序显示图像。

我想修改代码,以便在7天之内没有加载新图像的情况下,显示默认的“脱机相机”图像。

我尝试在最后使用array_push添加我的默认图像,但是我无法使其正常工作。有什么建议吗?


// Array holding filename and creation date
$files = array();
// Delete all webcam files older than a week
function getFileList()
{

global $files;

    $captchaFolder  = './';         // Define the folder to clean 
    $fileTypes      = 'EZ*.jpg';    // Filetypes to check 
    $expire_time    = 10080;        // Define after how many minutes the files should get deleted


    // Find all files of the given file type
    foreach (glob($captchaFolder . $fileTypes) as $Filename)
    {
        $FileCreationTime = filectime($Filename);   // Read file creation time
        $FileAge = time() - $FileCreationTime;      // Calculate file age in seconds
        if ($FileAge > ($expire_time * 60))         // Is the file older than the given time span?
        {
            unlink($Filename);                      
        }
        else
        {
            array_push($files, array($Filename, $FileCreationTime));
        }
    }
    // Sort the array by $FileCreationTime
    $times = array();
    foreach($files as $file)
    {
        $times[] = $file[1];
    }
    array_multisort($times, SORT_ASC, SORT_NUMERIC, $files);
}

getFileList();
$numPics = count($files);

// Insert the options in the select element
function buildOptions()
{
    global $files;
    $count = 1;
    foreach ($files as $file)
    {
        $dt =  date ("D d/m/y H:i", $file[1]);
        echo "\t\t<option value=\"$file[0]\">$dt (UTC)</option>\n";
        $count++;
    }
}

1 个答案:

答案 0 :(得分:0)

// check if the array is empty
if(empty($files)){

    // add a default image to the empty array (the timestamp doesn't matter because it will not be used)
    $files[] = array('default_image.png');

    // because it's empty you could also use:
    $files = array(array('default_image.png'));

}