{"record":{"id":"ef92bf881b86ddc8","repo":"octobercms/october","slug":"cms-lang-cms-object-file-already-exists","errorCode":null,"errorMessage":"cms::lang.cms_object.file_already_exists","messagePattern":"cms::lang\\.cms_object\\.file_already_exists","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"modules/cms/classes/Lang.php","lineNumber":257,"sourceCode":"                    ['name' => $key]\n                ));\n            }\n\n            $this->$key = $value;\n        }\n    }\n\n    /**\n     * save the object to the disk\n     */\n    public function save(array $options = [])\n    {\n        $this->validateFileName();\n\n        $fullPath = $this->getFilePath();\n\n        if (File::isFile($fullPath) && $this->originalFileName !== $this->fileName) {\n            throw new ApplicationException(LangHelper::get(\n                'cms::lang.cms_object.file_already_exists',\n                ['name'=>$this->fileName]\n            ));\n        }\n\n        $dirPath = $this->theme->getPath().'/'.$this->dirName;\n        if (!file_exists($dirPath) || !is_dir($dirPath)) {\n            if (!File::makeDirectory($dirPath, 0755, true, true)) {\n                throw new ApplicationException(LangHelper::get(\n                    'cms::lang.cms_object.error_creating_directory',\n                    ['name'=>$dirPath]\n                ));\n            }\n        }\n\n        $newFullPath = $fullPath;\n        if (@File::put($fullPath, $this->content) === false) {\n            throw new ApplicationException(LangHelper::get(","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/octobercms/october/blob/b608633a7e8922487d91a8161499020121c3b3bf/modules/cms/classes/Lang.php#L239-L275","documentation":"ApplicationException thrown by `Lang::save()` when the target file already exists on disk AND the new name differs from the original (`originalFileName !== fileName`), i.e. the save is a rename onto an existing file. The check deliberately prevents silently overwriting another theme language file.","triggerScenarios":"Renaming a lang file in the editor to a base name that is already present in `themes/<theme>/lang/` (e.g. renaming messages.json to en.json when en.json exists); creating a 'new' file whose name collides with one on disk.","commonSituations":"Two similar translation files consolidated by rename; case-insensitive filesystems (macOS/Windows dev) hiding EN.json vs en.json collisions that later break on Linux; files left behind by a previous import.","solutions":["Choose a different, unused file name for the rename.","If overwriting is intended, delete or rename the existing file first, or edit the existing file instead of creating a duplicate.","Pre-check with `File::exists($obj->getFilePath())` before calling save() and surface a friendly message."],"exampleFix":"// before — rename collides with an existing file\n$lang->fileName = 'en.json'; // lang/en.json already exists\n$lang->save(); // throws file_already_exists\n\n// after — verify the target is free first\n$target = $theme->getPath() . '/lang/' . $lang->fileName;\nif (File::exists($target) && $lang->originalFileName !== $lang->fileName) {\n    throw new ApplicationException('en.json already exists — pick another name.');\n}\n$lang->save();","handlingStrategy":"validation","validationCode":"use Winter\\Storm\\Support\\Facades\\File;\n\n$target = $lang->getFilePath();\nif (File::exists($target) && $lang->originalFileName !== $lang->fileName) {\n    throw new ValidationException(['fileName' => 'That file name is already taken in this theme.']);\n}\n$lang->save();","typeGuard":null,"tryCatchPattern":"try {\n    $lang->save();\n} catch (ApplicationException $e) {\n    if (strpos($e->getMessage(), 'already exists') !== false) {\n        return back()->with('error', 'Name taken — choose another file name.');\n    }\n    throw $e;\n}","preventionTips":["Check File::exists() on the target path before any rename-style save.","Offer the user a generated unique name (e.g. suffix -copy) when a collision is detected in the UI.","Use lowercase file names consistently to avoid case-insensitive filesystem surprises."],"tags":["filesystem","duplicate-file","lang","save","rename"],"backgroundTag":"file-already-exists","analyzedSha":"b608633a7e8922487d91a8161499020121c3b3bf","analyzedAt":"2026-08-21T04:24:57.515Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}