# php manage images

on
I have a web cam that uploads an image onto my web server every minute. Is there any way that I can use php to:
1. detect the most recent image that was loaded up and display it
2. delete the previous image
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hello Neil,

You can use following

http://php.net/manual/en/function.filemtime.php

Thanks.

Commented:
How are the files stored on your server? is each image created separately and saved with a different filename? one option would be to simply have it overwrite the same file each time, so the filename of the image would never change. The other option, to build on what amar said, would be something like this:

<?php

//$dir = "C:\\path\\to\\images\\"; // Windows Server$dir = "/path/to/images/";         // Linux/Unix Server

$newestFileName = "";$newestMTime = 0;

if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file{0} !== '.' && filemtime($dir .$file) > $newestMTime) {$newestMTime = filemtime($dir .$file);
$newestFileName =$file;
}
}
closedir($dh); } } echo "newest file:$newestFileName";
?>

Web UX/UI Developer

Commented:
Thanks for that I will trial it and see how I get on. Yes the images are stored on a folder each with their own name.
Web UX/UI Developer

Commented:
That works well. How would I then delete all preceding images (leaving only the latest)?
Commented:
Two choices. The fastest/best would be to use a system command... but under the assumption that you cannot make system calls from PHP on your server, you could use the PHP unlink function. I've modified the code above to remove everything in the directory that is not the most current:

<?php

//$dir = "C:\\path\\to\\images\\"; // Windows Server$dir = "/path/to/images/";         // Linux/Unix Server

$newestFileName = "";$newestMTime = 0;

if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file{0} !== '.') {
if (filemtime($dir .$file) > $newestMTime) {$newestMTime = filemtime($dir .$file);
$newestFileName =$file;
} else {
unlink($dir .$file);
}
}
}
closedir($dh); } } if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file{0} !== '.' &&$file !== $newestFileName) { unlink($dir . $file); } } closedir($dh);
}
}

echo "newest file: $newestFileName"; ?>  It looks longer than it would normally have to be but is a little bit more efficient for a directory with a lot of files (only really useful the first time it runs I guess) Web UX/UI Developer Commented: A perfect solution.. Thanks you for such a thorough answer. I may open this up as another question but how easy would it be to keep the last 5 most recent images (deleting the rest). I am thinking that if I can get the image reference information ($newestFileName
)into an array ( foreach etc) I can output them as a nice image sequence.

Commented:
absolutely. That wouldn't be too difficult at all. I'll throw something together for you and post it in your new question

Commented: