在CakePHP中上传文件的最佳做法 [英] Best practice to upload files in CakePHP

查看:1076
本文介绍了在CakePHP中上传文件的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议我在CakePHP中上传文件的最好方式。

Can any one suggest me the best way to write code for uploading a file in CakePHP?


  • 我需要上传文件

  • 如果记录保存失败,则不应上传文件。

  • 如果上传失败,则记录应回滚

  • 代码应该可重用

  • 我需要上传 afterSave

  • 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

推荐答案

编辑(2015年):请参阅当前文件插件的awesome CakePHP 列表(2.x分支此处

Edit (2015): Please see the Awesome CakePHP list for current file plugins (2.x branch here)

编辑(2013):此答案现在有点过时了,因此我只会留下所有上传插件的列表:

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)

原始答案(自2010年):

Original answer (from 2010):


您可以考虑使用媒体插件在最后一次CakeFest上讨论过。它是相当全面的,包含创建文件变体(例如图像或视频的缩略图),帮助输出不同类型的文件和元素,以帮助创建多文件上传表单更快的功能;并从一开始就非常注重安全性,因为我可以从收集幻灯片。我在生产中使用它,并发现它工作得很好,加上它似乎更多的积极发展比其他选项,我已经看到,如果这是要去的(例如, MeioUpload行为上传插件 FileUpload插件和关于该主题的各种面包店/博客帖子。)

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

这篇关于在CakePHP中上传文件的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆