php manage images

Neil_Bradley
Neil_Bradley used Ask the Experts™
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
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hello Neil,

You can use following

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

It will help you in resolving your problem.

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";
?>

Open in new window

Neil_BradleyWeb UX/UI Developer

Author

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.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Neil_BradleyWeb UX/UI Developer

Author

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";
?>

Open in new window


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)
Neil_BradleyWeb UX/UI Developer

Author

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:
in fact, I have an answer for you already ;-)  
Neil_BradleyWeb UX/UI Developer

Author

Commented:
standby for a new question to be added..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial