Link to home
Start Free TrialLog in
Avatar of Neil_Bradley
Neil_BradleyFlag for New Zealand

asked on

php manage images

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
Avatar of Amar Bardoliwala
Amar Bardoliwala
Flag of India image

Hello Neil,

You can use following

It will help you in resolving your problem.

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:


//$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;

echo "newest file: $newestFileName";

Open in new window

Avatar of Neil_Bradley


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.
That works well. How would I then delete all preceding images (leaving only the latest)?
Avatar of m4trix
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
absolutely. That wouldn't be too difficult at all. I'll throw something together for you and post it in your new question
in fact, I have an answer for you already ;-)  
standby for a new question to be added..