PHP Coding: Five hours of work for ten lines of code

It’s taken me 5-hours of effort over the last day or two to get my webcam correctly reporting a picture for my weather station.

Problem:  The Wunderground FTP process has stopped working with no sign of a resolution. I needed to provide a single image that could be referenced by Wunderground rather than FTP to their site. The trouble was that my webcam uses incremental filenames based on date-timestamp. I also needed to use the latest and get rid of any other uploads.

Solution: Use the PHP code that runs my weather page and forwards the weather station page to Wunderground to, in addition, find the latest image file and after 5-minutes rename it to the static name image file and delete all the other potentially uploaded webcam files.

It took a lot of trial and error to get to the code below. All the while reading the online PHP reference and other PHP examples to get to what I needed. It’s not necessarily petty code but it does work.

<?php
// list the ARG*.jpg files in the webcam directory. Save to an array
$ARCfiles = glob('../webcam/ARC*.jpg');
// reverse sort the array to get the most recent file by timestamped name
rsort ($ARCfiles);
// grab the first (most recent file)
$fileselected = $ARCfiles[0];
// setup the logging file and write the selected file to the logging file
// $latestfile = '../webcam/latestwebcamfile.txt';
// file_put_contents($latestfile, $fileselected);
// check that the webcam image is 5-minutes old. If so overwrite it with the latest and delete all other ARC*.jpg files.
if(file_exists('../webcam/webcamimage.jpg')&&time()- filectime('../webcam/webcamimage.jpg')>300){&nbsp; &nbsp;
   rename($fileselected , '../webcam/webcamimage.jpg');&nbsp; &nbsp;
   // list then delete every file beginning with ARC*&nbsp; &nbsp;
   $files = glob('../webcam/ARC*.jpg');&nbsp; 
   foreach($files as $file){ 
      if(is_file($file)){ 
         unlink($file); 
      }&nbsp; &nbsp;
   }
}
?>

Loading