add custom filename option #93

wants to merge 9 commits into

5 participants


hi and thanks for great plugin.
i explained about how to use custom filename option in readme file. my English is not good so maybe i missed something in readme.

i added 2 function to UploadBehavior and modified afterSave to add this feature.
there is several ways to implement custom filename, i decide to use Pattern. its very simple and easy to use.

for example this pattern :


will return something like this : orginal_name_1.
you can create function in your model and call it to generate custom filename:

class Model extends AppModel {
    public function myMethod($filename = ''){
        return md5($filename);

in above example suffix is just a simple text. and $filename is will be passed to each method you call in Pattern.

i used sanitizeFilename function to remove unsupported character from filename.


filename changed currectly , but in database , i have the orginal name instead custom filename, so , what you prefer ? update again ? or should i move actions from afterSave to beforeSave ?


You have mistake in document. In your example it's not working.
Shoul be:
'photo' => array(
'fields' => array(
'dir' => 'photo_dir',

'customName' => '{!getName}'


@vxtrim where exactly ?


Thanks for taking the time to solve this problem!

I have an issue with the code though, I wanted to use the md5 hash for a file name (the last option). However, my filenames are all renamed to "1." Also, the new filename is not saved to my database.

Any ideas? Thanks!!


@awtprod hi, in last commit i fixed the database problem.

Sorry i didn't see the gist :)

now i find out you did not apply the last commit :)


That worked! Thanks!

However, I can't figure out why the filename is always changed to "1." Is it because it's not reading the filename in my model?


if filename changed in database correctly so you must have the exact filename.
in line 288 check $filePath value, and make sure you are in right path.


I'm not quite sure what you mean by that....I haven't changed that line. $path is set to the default path.


Great improvement intuxicated. Any idea on how I could set the file name to be the same as the id of the entry it is going to be stored in the table?


@wink- sorry that's not possible now , but you can use afterSave method

jlis and others added some commits Mar 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment