1

I have a folder of images that I am trying to change to be the date and time the picture was taken. I managed to make it work for the most part, however if the image has the same DateTimeOriginal (to the second), the subsequent images are deleted and replaced with the last one. This is a problem when I use burst on my camera.

I am trying to have the code add "_1" after each file name, unless the file name exists, then I want the "_1" to increase by 1. So far, the code I have will catch the first duplicate name and work properly, but every other matching filename after just deletes the file before it that has the same name (which was just renamed by the code).

In case it makes a difference, I am using XAMPP to run the PHP code in a local directory on my windows 10 PC, but I do test it online as well and have the same outcome.

The following is the code I have come up with by piecing together other code that I have found, and then attempting to customize it. I have a general understanding of PHP through trial and error, but have no education. I feel like I should be using a "while" statement while(file_exists('pictures/'.$timestamp.'_'.$count.'.jpg')) instead of or in conjunction with the current if statement I have if (file_exists('pictures/'.$timestamp.'_'.$count.'.jpg'))

The entire code I am using is here:

<?php
$count=1;
$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
    while($file = readdir($handle)){
        if($file !== '.' && $file !== '..'){
            date_default_timezone_set('America/Edmonton');      
            $dirloc = "pictures/$file";
            $newdirloc = "NewPictures/$file";
            $exif_data = exif_read_data ($dirloc, $file, 0, true);
            $exifString = $exif_data['DateTimeOriginal'];
            $exifPieces = explode(":", $exifString);
            $newExifString = $exifPieces[0] . "-" . $exifPieces[1] . "-" . $exifPieces[2] . ":" . $exifPieces[3] . ":" . $exifPieces[4];
            $exifTimestamp = strtotime($newExifString);
            $timestamp = date("y-m-d_H-i-s", $exifTimestamp);
            if (file_exists('pictures/'.$timestamp.'_'.$count.'.jpg')) {
                $ExistingFile = 'pictures/'.$timestamp.'_'.$count.'.jpg';
                $delimiters = ['-', '_', '.'];
                $newStr = str_replace($delimiters, $delimiters[0], $ExistingFile);
                $NamePieces = explode("-", $newStr);
                $count = $NamePieces[6];

                ++$count;
                echo ($file.' has now been changed to '.$timestamp.'_'.$count.'.jpg (Increased count from existing file)<br>');
                rename('pictures/'.$file, 'pictures/'.$timestamp.'_'.$count.'.jpg');
            }
            else {
                echo ($file.' has now been changed to '.$timestamp.'_1.jpg (Unique File)<br>');
                rename('pictures/'.$file, 'pictures/'.$timestamp.'_1.jpg');
            }
        }
    }
?>

Thanks for helping this newbie figure this out!

Edit: I think I've narrowed it down to a simpler question. If it is possible to see if $ANY_NUMBER is in fact any number, what would I say $ANY_NUMBER is = to? With this change, I should be able to get rid of the count=1 at the start, and if it is true that it is any number in that [6] spot, than I should be able to say that count= that [6] spot. Does that make sense?

if (file_exists('pictures/'.$timestamp.'_'.$ANY_NUMBER.'.jpg')) {
 $ExistingFile = 'pictures/'.$timestamp.'_'.$ANY_NUMBER.'.jpg';
 $delimiters = ['-', '_', '.'];
 $newStr = str_replace($delimiters, $delimiters[0], $ExistingFile);
 $NamePieces = explode("-", $newStr);
 $count = $NamePieces[6];
 ++$count;
 
 echo ($count);
}
  • You need a recursive check to find the last integer and then increment it, if not match in found, not need of integer, just create. – user3783243 Feb 26 '22 at 00:35
  • Use a loop (e.g another while block) to increment the filename suffix (if same exists) . So if there are say three files with the time 2020-02-26 10:12:30 , then the 1st one will be 2020-02-26-10:12:30.jpg, the next one will be 2020-02-26-10:12:30_1.jpg (checking that 2020-02-26-10:12:30 exists, so add _1, now not exists then use it) and the final one will be 2020-02-26-10:12:30_2.jpg (checking that 2020-02-26-10:12:30 exists, so add _1, now still exists, change to _2, now not exists then use it) . Obviously you need a loop to increment the suffix. See ? – Ken Lee Feb 26 '22 at 03:17
  • user3783243 Do you know how to do this? Please see my edit. – TurboCanuck Feb 26 '22 at 08:51

0 Answers0