Skip to content

Instantly share code, notes, and snippets.

@lda-art
Last active December 29, 2023 13:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lda-art/1707383c156d57c1b5f0ae8748eab815 to your computer and use it in GitHub Desktop.
Save lda-art/1707383c156d57c1b5f0ae8748eab815 to your computer and use it in GitHub Desktop.
Octobercms rename upload files
use System\Classes\MediaLibrary;
use Event;
public function boot()
{
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
$originalName = $uploadedFile->getClientOriginalName();
$cyr = [
'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п',
'р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я',
'А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П',
'Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я'
];
$lat = [
'a','b','v','g','d','e','io','zh','z','i','y','k','l','m','n','o','p',
'r','s','t','u','f','h','ts','ch','sh','sht','a','i','y','e','yu','ya',
'A','B','V','G','D','E','Io','Zh','Z','I','Y','K','L','M','N','O','P',
'R','S','T','U','F','H','Ts','Ch','Sh','Sht','A','I','Y','e','Yu','Ya'
];
$cyr2lat = str_replace($cyr, $lat, $originalName);
$sanitizedFileName = str_replace(' ', '-', $cyr2lat);
$filePathChunks = explode(DIRECTORY_SEPARATOR, $filePath);
$filePathChunks[ (count($filePathChunks) - 1) ] = $sanitizedFileName;
$newPath = implode(DIRECTORY_SEPARATOR, $filePathChunks);
$isRename = true;
MediaLibrary::instance()->moveFile($filePath, $newPath, $isRename);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment