Sanitize Uploaded File Name

is there short way in Yii to make a name file valid for all server ?

for example if the uploaded file name is "το αρχείο μου.jpg" the urlτο αρχείο μου.jpg will be not recognized properly (or the image link will be corrupted)

I found something similar (sanity url) but I want for files to rename like that

"το αρχείο μου.jpg" => "to_arxeio_mou.jpg"

the same function I want to work (if it is possible) for all languages like Chinese Russian etc

if there is no way in Yii (API) how to do that in easy way with native PHP code ?


i think you may change the image name random generate.

for r.g

αρχείο μου.jpg => 869298996.jpg 

and this name store the db so it’s easily manage.

Hi Ankit and thanks for the reply

I know that way by hashing of the name.

But I want something that the user could remember what is the file by its name.

Any other suggestion ?

If you’re going to convert filename anyway, remembering original name is useless for user.

But you can use some function like md5($name), and if someone is requesting “αρχείο μου.jpg” you just return md5(‘αρχείο μου’) . ‘.jpg’ to him.

(Bad news is you’ll probably need to write webserver module to convert file names on the fly)

You need the intl extension that uses the awesome ICU library.

Check out the latest comment, it gives a working example:

function slugify($string) {

    $string = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $string);

    $string = preg_replace('/[-\s]+/', '-', $string);

    return trim($string, '-');


echo slugify("Я люблю PHP!");

As for the unique filenames I just create a simple table to hold an id, filename and hash. Files are stored on disk using that hash. I can reference a file by whatever I need.

Hi ORey

As I said to Amkit, I don’t want this way :)

It is not absolutely useless to User if filename seems like the native (matching each letter of native to latin letter α=>a β=>v γ=>g etc)

In Greece this called greeklish and a lot of people use it (at least in chat). I think this is used and in other nationalities that has no-latin characters.

Hi nineinchnick

Interesting… I check it and I inform you :)