Tuesday, February 21, 2012

CakePhp file upload plugin

After much, MUCH googling i found this awesome file upload plugin. It has a great thumbnail feature and some really good validation options.

https://github.com/josegonzalez/upload/tree/2.0

However I found out you cant randomize the file name. (There is an option to randomize the folder it store the file in). After a bit of hack and slash i managed to append a random string 'time()' to file name.

/app/Plugin/Upload/Model/Behavior/UploadBehavior.php

//Line 78
private $rand_str;

//Line 87(inside the setup function)
$this->rand_str =  time();
//End of the beforeSave function
$model->data[$model->alias][$field] = $this->rand_str."_".
$model->data[$model->alias][$field];

This will append the UNIX time stamp to the file you're uploading as well the file name stored in the table and all the thumbnails.

No comments: