From 798f30ea515a25214c385733960f5cbc804f8b1a Mon Sep 17 00:00:00 2001 From: plegall Date: Tue, 15 Jul 2025 14:14:42 +0200 Subject: [PATCH] issue #2390 avoid generating duplicate paths during upload --- admin/include/functions_upload.inc.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php index b5eb59b35..3ea729e1b 100644 --- a/admin/include/functions_upload.inc.php +++ b/admin/include/functions_upload.inc.php @@ -227,7 +227,7 @@ SELECT // compute file path $date_string = preg_replace('/[^\d]/', '', $dbnow); - $random_string = substr($md5sum, 0, 8); + $random_string = substr($md5sum, 0, 4).'%s'; $filename_wo_ext = $date_string.'-'.$random_string; $file_path = $upload_dir.'/'.$filename_wo_ext.'.'; @@ -270,6 +270,16 @@ SELECT } prepare_directory($upload_dir); + + $file_path_pattern = $file_path; + do + { + // we generate a random string for each upload. If the user uploads + // the same photo twice at the same time (same timestamp, same md5sum) + // we still want the path to be unique. + $file_path = sprintf($file_path_pattern, substr(bin2hex(random_bytes(4)), 0, 4)); + } + while (file_exists($file_path)); } if (is_uploaded_file($source_filepath))