Am I right? Deleting and renaming within a folder.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
flyingcolours
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Jan 06, 2020 12:51 am

Mon Jan 06, 2020 1:00 am

Hey everyone,

I have a camera uploading a snapshot every ten minutes to a folder on a wordpress site. I need to run a script each time someone visits the site that deletes all previous images except the most recent image and renames this recent image to something I can link to. Or failing that, I need to just display the most recent image on the site, and delete all the older images periodically to save space.

At this stage its a one page site, using the index.php page.

I've been found this script but not sure if it's correct or where to insert it.

Code: Select all

<!--Sample webpage.php deletes all but newest .jpg and strips the timestamp -->
<!-- Assumes this web page is in the /webcam folder -->
<!-- Assumes your specified jpg name is 8 characters e.g. foscam-1.jpg -->
<!-- Your web page name needs to have a .php file extension -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Web Cam Display 1</title></head>

<body>

<?php
<!—Change the next line for your snap directory -->
chdir ('FI9826P_00626E67204E/snap'); 
$files = glob('*.jpg'); 
if(count($files) < 2){ 
   return 0;
 }

$file = array_shift( $files);
$mTime = filemtime( $file);

foreach($files as $currFile){ 
  $currTime = filemtime($currFile);
  if( $currTime > $mTime){ 
    unlink($file); 
    $file = $currFile;  
    $mTime = $currTime;
 } else {unlink($currFile); }
 }
  
rename($file, substr($file, 0, 8) . '.jpg'); 
?>

<!--The rest of your web page HTML goes here -->
<!—Change the next line for your snap directory -->
<p><img src="FI9826P_00626E67204E/snap/foscam-1.jpg"></p>

</body>
</html>
Any help would be appreciated.
Post Reply