diff --git a/app/Libraries/MyStorage/FileLibrary.php b/app/Libraries/MyStorage/FileLibrary.php index cf7e9f7..6cb0b75 100644 --- a/app/Libraries/MyStorage/FileLibrary.php +++ b/app/Libraries/MyStorage/FileLibrary.php @@ -46,9 +46,8 @@ class FileLibrary extends MyStorageLibrary $this->makeDirectory($fullPath); $saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $this->getOriginName(); if (file_exists($saveFilePath)) { - $fileInfos = pathinfo($saveFilePath, PATHINFO_ALL); - $saveFile = base64_encode($fileInfos['filename']) . "." . $fileInfos['extension']; - $saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $this->getOriginName(); + $saveFile = $this->getUniqueFilename($fullPath, $this->getOriginName()); + $saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $saveFile; log_message("notice", __FUNCTION__ . "파일명 변경 : 원본파일 {$this->getOriginName()}->저장파일 {$saveFile}"); $this->setOriginName($saveFile); } diff --git a/app/Traits/FileTrait.php b/app/Traits/FileTrait.php index 5f3ba34..d575ff1 100644 --- a/app/Traits/FileTrait.php +++ b/app/Traits/FileTrait.php @@ -44,4 +44,19 @@ trait FileTrait } return $files; } + + final public function getUniqueFilename($path, $file_name) + { + $fileExtension = pathinfo($file_name, PATHINFO_EXTENSION); + $fileBaseName = pathinfo($file_name, PATHINFO_FILENAME); + $newFilename = $file_name; + $counter = 1; + // 중복된 파일명이 존재하는지 확인 + while (file_exists($path . DIRECTORY_SEPARATOR . $newFilename)) { + // 중복된 파일명이 존재하면 숫자를 추가하여 새로운 파일명 생성 + $newFilename = $fileBaseName . '_' . $counter . '.' . $fileExtension; + $counter++; + } + return $newFilename; + } }