Best practice to upload files in CakePHP

蓝咒 提交于 2019-11-27 11:22:37

问题


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

回答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:

  • josegonzalez/upload
  • milesj/Uploader
  • szajbus/uploadpack
  • webtechnick/CakePHP-FileUpload-Plugin (2.0-branch)
  • bmcclure/CakePHP-Media-Plugin (2.0-branch)
  • davidpersson/media (1.3.x-only)
  • jrbasso/MeioUpload (deprecated)

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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/3148203/best-practice-to-upload-files-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!