Best practice to upload files in CakePHP

20,114

Solution 1

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):

You might consider using the Media plugin that was talked about at the last CakeFest. It is fairly comprehensive, containing features such as creating file variations (ie. thumbnails of images or video), helpers for outputting different types of files, and elements to help create multiple-file upload forms more quickly; and also had a strong focus on security from the outset as I can gather from the slides. I use it in production and find it works well, plus it seems to be under more active development than other options I have seen if that's anything to go by (eg. MeioUpload behavior, Uploader plugin, FileUpload plugin and various Bakery/blog posts on the topic).

Solution 2

Take a look at the File fields section in the Cookbook.

Solution 3

I found this https://github.com/srs81/CakePHP-AjaxMultiUpload plugin helpful for my case.

Share:
20,114
RSK
Author by

RSK

Rubyist / JavaScripter Ex Yeoman Backbone generator maintainer Open source enthusiast Blogger @ PHPRepo and personal blog Coder @ Codemancers Find my presence on web

Updated on August 05, 2020

Comments

  • RSK
    RSK over 3 years

    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
  • Michał Szajbe
    Michał Szajbe about 12 years
    Let me shamelessly add Upload Pack to the list
  • Anthony
    Anthony almost 12 years
    Uploader and Upload Pack are the top 2 supported CakePHP 2.x libs that I have found after half a day of research and attempts at implementation.
  • Eirik H
    Eirik H about 11 years
    Upload Plugin 2.0 by Jose Diaz-Gonzalez seems like a good choice now.