It seems that some of our users are uploading filenames with foreign characters...
Is there any way to prevent this?
I'd like to keep the filenames to all English letters (plus numbers, dash & underscore).
(If relevant, this is for Craft 2.)
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityIt seems that some of our users are uploading filenames with foreign characters...
Is there any way to prevent this?
I'd like to keep the filenames to all English letters (plus numbers, dash & underscore).
(If relevant, this is for Craft 2.)
Cant you just use preg_replace("/[^A-Za-z0-9 ]/", '', $stuff)
for this? Seems that will strip away anything that isnt a number or a letter from the english alphabet?
EDIT --------- Challenge accepted ;)
public function modifyAssetFilename($filename){
return preg_replace("/[^A-Za-z0-9-]/", '', $filename);
}
Something according to the above strips out all unwanted characters using the hook if you place it in a plugins main class file. The above preg_replace should also keep dashes e.t.c.
Another way you might be able to approach the sanitation of filenames is listening to the onBeforeSaveAsset
method and then using craft()->assets->renameFile()
once you have sanitized it. Not sure which one is better. Both should work.
modifyAssetFilename
hook, I would mark it as correct.
– Lindsey D♦
Mar 21 '18 at 19:41