Can any one suggest me the best way to write code for uploading a file in CakePHP?
- I need to upload a file and save its name in table.
- If the record saving fails it should not upload the file.
- If the uploading fails then the record should be rollbacked.
- The code should be reusable
- I need to upload the file in
afterSave
callback
Edit (2015): Please see the Awesome CakePHP list for current file plugins (2.x branch here)
Edit (2013): This answer is now a bit dated, so I will just leave a list of all mentioned upload plugins:
Original answer (from 2010):
Take a look at the File fields section in the Cookbook.
I found this https://github.com/srs81/CakePHP-AjaxMultiUpload plugin helpful for my case.