I have an upload folder that has images inside of it that are named in this format...
10000XXXX-COUNT-DATE-EMPLOYEE-PP-FILEEXTENSION
which looks like this...
100002246-1-2014-05-05-David.Rosales-PP.jpg
Each time a photo is uploaded using PHP I need to scan the files in this directory and find a match for the ID part which is the first 9 digits of the filename.
IF a match is found, I then need to append a number to the COUNT
position which is in the number 11 character spot from the left.
The idea is an ID can have 5-6 images. If the ID has 1 image already then the next image should be named 100002246-2-2014-05-05-David.Rosales-PP.jpg
and after that another image on a different upload sessions would be 100002246-3-2014-05-05-David.Rosales-PP.jpg
so each new image for an ID number will increment by 1 number. You can see I have added the count number at the number 11 character spot.
I am not sure how to best achieve this right now. I cannot use a database so I have to do this during the upload process and scan the directory to find any matches for an image.
Any ideas and example of how I can best do this please?
Something like this for scanning the directory looks promising...
foreach (new DirectoryIterator('.') as $item) {
echo $item, PHP_EOL;
}
UPDATE - My attempt at a solution so far
<?php
$orderId = $_GET['orderid'];
$dir = new DirectoryIterator('uploads');
$fileMatches = array();
foreach ($dir as $fileinfo) {
$filenameId= substr($fileinfo->getFilename(), 0, 9);
if($filenameId === $orderId){
//store File ID matches in an array
$fileMatches[] = $filenameId;
}
echo $fileinfo->getFilename() . "<br>";
}
$totalImages = count($fileMatches);
echo '<hr>Total Images found for this ID '.$orderId.' = '.$totalImages;
echo '<hr>New count for image would be = '. ($totalImages+1);
?>