在 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
推荐答案
Edit (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-分支)
- bmcclure/CakePHP-Media-Plugin (2.0-branch)
- davidpersson/media(仅限 1.3.x)
- jrbasso/MeioUpload(已弃用)
- 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 插件 以及有关该主题的各种 Bakery/博客文章).
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).
相关文章