How to rename uplaoded file ? #12763
Answered
by
wsydney76
vitalijalbu
asked this question in
Q&A
-
Hi guys, anyone can help me with a simple task? I'd like to rename every file to random md5 after uploading, upload works but doesn't rename...
|
Beta Was this translation helpful? Give feedback.
Answered by
wsydney76
Mar 1, 2023
Replies: 1 comment 2 replies
-
Used this piece of code, when we had to obfuscate filenames, maybe it helps: Event::on(
Asset::class,
Element::EVENT_AFTER_SAVE,
function($event) {
/** @var Asset $asset */
$asset = $event->sender;
if (! ... skip if this file should not be renamed... .) {
return;
}
$newFilename = "$asset->uid.$asset->extension";
// This filename exists already
if (Asset::find()
->folderId($asset->folderId)
->filename($newFilename)
->exists()) {
return;
}
Craft::$app->assets->moveAsset($asset, $asset->folder, $newFilename);
}
); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
brandonkelly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used this piece of code, when we had to obfuscate filenames, maybe it helps: