From f3540a2546f3b962fc0765a75245092aa32bf488 Mon Sep 17 00:00:00 2001 From: Alex <40072887+alexdcrane@users.noreply.github.com> Date: Tue, 23 Dec 2025 14:22:59 -0700 Subject: [PATCH] Fix: Correctly find the next available voicemail greeting id (#7678) when uploading to avoid using the same name as another greeting. --- .../voicemail_greetings.php | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/voicemail_greetings/voicemail_greetings.php b/app/voicemail_greetings/voicemail_greetings.php index 24e24effcb..3863d05ee8 100644 --- a/app/voicemail_greetings/voicemail_greetings.php +++ b/app/voicemail_greetings/voicemail_greetings.php @@ -174,11 +174,32 @@ $file_ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $file_name = $_FILES['file']['name']; - //check file extension - if ($file_ext == 'wav' || $file_ext == 'mp3' || $file_ext == 'ogg') { + //define the allowed file extensions + $allowed_extensions = ['wav', 'mp3', 'ogg']; - //get the next greeting id starting at 1 - $greeting_id = count(glob($greeting_dir . '/greeting_*.*')) + 1; + //check file extension + if (in_array($file_ext, $allowed_extensions)) { + + //find the next available greeting id starting at 1 + $greeting_id = 1; + while (true) { + $found_existing_file = false; + + //check for wav, mp3, and ogg files with the current greeting id + foreach ($allowed_extensions as $extension) { + $potential_file_name = "greeting_{$greeting_id}.{$extension}"; + if (file_exists($greeting_dir . '/' . $potential_file_name)) { + $found_existing_file = true; + break; + } + } + + if (!$found_existing_file) { + break; + } + + $greeting_id++; + } //set the greeting file name $greeting_file_name = "greeting_{$greeting_id}.{$file_ext}";