octobercms/october · error · ApplicationException

backend::lang.media.folder_or_file_exist

Error message

backend::lang.media.folder_or_file_exist

What it means

Thrown by onCreateFolder after MediaLibrary::instance()->folderExists($newFolderPath) returns true. The handler builds the target as validatePath(path) . '/' . name and refuses to create a folder that already exists, so the library never overwrites or merges directories.

Source

Thrown at modules/media/widgets/MediaManager.php:509

        $name = trim(Input::get('name'));
        if (!strlen($name)) {
            throw new ApplicationException(Lang::get('cms::lang.asset.name_cant_be_empty'));
        }

        if (!$this->validateFileName($name)) {
            throw new ApplicationException(Lang::get('cms::lang.asset.invalid_name'));
        }

        $path = Input::get('path');
        $path = MediaLibrary::validatePath($path);

        $newFolderPath = $path.'/'.$name;

        $library = MediaLibrary::instance();

        if ($library->folderExists($newFolderPath)) {
            throw new ApplicationException(Lang::get('backend::lang.media.folder_or_file_exist'));
        }

        /*
         * Create the new folder
         */
        if (!$library->makeFolder($newFolderPath)) {
            throw new ApplicationException(Lang::get('backend::lang.media.error_creating_folder'));
        }

        /**
         * @event media.folder.create
         * Called after a folder is created
         *
         * Example usage:
         *
         *     Event::listen('media.folder.create', function ((\Media\Widgets\MediaManager) $mediaWidget, (string) $newFolderPath) {
         *         \Log::info($newFolderPath . " was created");
         *     });

View on GitHub (pinned to b608633a7e)

Solutions

  1. Refresh the media list and create the folder under a different name
  2. Guard against double submission (disable the button until the AJAX completes)
  3. If scripting, call MediaLibrary::instance()->folderExists($path) (or listAllDirectories) before invoking the handler

Example fix

// before
$.request('onCreateFolder', { data: { path, name } });

// after
const exists = folders.some(f => f === (path === '/' ? '' : path) + '/' + name);
if (!exists) {
    $.request('onCreateFolder', { data: { path, name } });
}
Defensive patterns

Strategy: validation

Validate before calling

// Client: check against the already-loaded folder list before requesting
const target = (path === '/' ? '' : path) + '/' + name;
if (knownFolders.some(f => f === target)) { showToast('Folder already exists'); return; }

Type guard

function folderIsUnique(name, path, knownFolders) {
  const target = (path === '/' ? '' : path) + '/' + name;
  return !knownFolders.includes(target);
}

Try / catch

$.request('onCreateFolder', { data }).fail(function (xhr) {
    if (/exist/i.test(String(xhr.responseJSON?.X_OCTOBER_ERROR_MESSAGE?.[0]))) refreshMediaList(); // stale list
    else showFlash('Create folder failed');
});

Prevention

When it happens

Trigger: AJAX 'onCreateFolder' where path.'/'.name already exists in the media library storage (e.g. creating 'photos' inside '/' when storage/app/media/photos exists), or two users/steps creating the same folder concurrently.

Common situations: Stale folder list in the browser (folder was created in another tab or by another user); leftover folder on disk after a record was deleted; race between a form submit and a duplicate submit (double click).

Related errors


AI-assisted analysis of octobercms/october@b608633a7e (2026-08-21). Data as JSON: /api/errors/d9c2696f50493dd4. Report an issue: GitHub.