<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
error_reporting(E_ALL);
// Seconds
$refreshRate = 10;

$path = getcwd() . DIRECTORY_SEPARATOR . 'snapshot0' . '.jpg';
$pathBackup = $path . '.backup';

if ( ! file_exists($path)) {
    // No file exists. Fetch but no backup.
    $attemptFetch = true;
} elseif (time() - filemtime($path) > $refreshRate) {
    // File exists, but is old. Fetch and backup.
    $attemptFetch = true;
} else {
    // File exists, but is still valid. Do nothing.
    $attemptFetch = false;
}
clearstatcache();
if ($attemptFetch) {
    exec("/usr/bin/env rm snapshot0.jpg");
    exec("/usr/bin/env wget -O snapshot0.jpg http://user:password@IP.ADD.RE.SS/cgi-bin/snapshot.cgi?channel=");
    }
header('Content-Type: image/jpeg');
readfile($path);