Question

I am trying to get my head around the documentation for installation and use of this Uploader plugin for CakePHP 2.x which deals with uploading of files. I have a few questions I hope somebody could help me with. There's quite a good thread on the topic, Installing Uploader Plugin for CakePHP 2.x, but I wanted to clear some things out which are not that clear in the thread (to me at least).

Btw the authors page: http://milesj.me/code/cakephp/uploader

Is it right that if you want to save manually (without the behaviour), the installation is as follows:

CakePlugin::load('Uploader'); // THIS GOES INTO THE BOOTSTRAP.PHP FILE PER THE REFERENCED QUESTION ANSWER
App::import('Vendor', 'Uploader.Uploader'); // THIS GOES INTO CONTROLLER PER THE REFERENCED QUESTION ANSWER
$this->Uploader = new Uploader(); // THIS GOES INTO CONTROLLER PER THE REFERENCED QUESTION ANSWER

Is that right?

BUT, if you are using the behaviour instead you don't do ANY of that above for installation, instead you do ONLY in model:

public $actsAs = array('Uploader.Attachment');

Is that right?

Is chapter 2 of the documentation only applicable for when using the plugin manually? Otherwise, you configure the model behaviour per chapter 3 only in the model.

Final question: So if I understand this right (I am still a little new to CakePHP), if I am using the behaviour to do the save automatically, whenever I upload a file to that model, it will automatically save the file in the right folder without me having to do anything and put a reference in the $data variable which is pointing to the saving location?

Thanks in advance!

Was it helpful?

Solution

I had such trouble getting this to work and understanding the whole plugin and behaviour thing so I thought it would be worth sharing quickly what I have learnt in a short video on how to install, implement and use the Uploader plugin through the model behaviour.

http://www.youtube.com/watch?v=lMNUOz8wqzE

Hope you find it helpful. To answer my questions above, I basically only have to:

  1. Include the CakePlugin::load('Uploader'); in the bootstrap.php file which can be found under App/Config/
  2. Call the behaviour inside my model as per public $actsAs = array('Uploader.Attachment'); but with the relevant options configured
  3. Then just save to the model, through my controller, and in the model I have included the $actsAs variable

The beforeSave callback function which has been defined in the Attachment behaviour in the Plugin will take care of the rest.

Uploading file seems to be such a mundane tasks so I suppose it is very appropriate to do it through a behaviour, and I don't want to write my own behaviour given my own beginners level so it's good that Miles has, particularly since he's an experienced developer. After reading up, uploading files using controller code is not the way to go, using expert developers plugin's probably is THE way to go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top