FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

How to rename file ? #435

Closed fcamargo10 closed 7 years ago

fcamargo10 commented 7 years ago

Hi, i don't know how to rename the file to save. Can you help me ?

Spriz commented 7 years ago

Did you see: https://github.com/josegonzalez/cakephp-upload/blob/21cef97798d82da9c31afdb3b1a246a453271665/docs/configuration.rst? :) It says something about renaming

fcamargo10 commented 7 years ago

I see, but i don't know how to use. Can u help me ? Look my example:

$this->addBehavior('Josegonzalez/Upload.Upload', [ 'icon' => [ 'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}', ] ]);

The upload is correct, but ... when I upload a file with the same name it is overwritten.

josegonzalez commented 7 years ago

You need to specify the nameCallback option.

fcamargo10 commented 7 years ago

Fine. Can u give me a example ? It is a string or a function ?

davidyell commented 7 years ago

It is a method which the behavior can call. It's listed in the documentation.

// In your Table class
$this->addBehavior('Josegonzalez/Upload.Upload', [
   'nameCallback' => [$this, 'renameFile'],
]);

// Table method in the same table the behaviour is loaded on
public function renameFile(array $data, array $settings): string
{
    $newFilename = str_replace('Example', '', $data['file']);
    return $newFilename;
}