php 在 CakePHP 中上传文件的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3148203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice to upload files in CakePHP
提问by RSK
Can any one suggest me the best way to write code for uploading a file in CakePHP?
任何人都可以建议我编写用于在 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
afterSavecallback
- 我需要上传一个文件并将其名称保存在表中。
- 如果记录保存失败,则不应上传文件。
- 如果上传失败,则应回滚记录。
- 代码应该是可重用的
- 我需要在
afterSave回调中上传文件
回答by deizel
Edit (2015):Please see the Awesome CakePHPlist for current file plugins (2.x branch here)
编辑(2015):请查看当前文件插件的Awesome CakePHP列表(2.x 分支在这里)
Edit (2013):This answer is now a bit dated, so I will just leave a list of all mentioned upload plugins:
编辑(2013):这个答案现在有点过时了,所以我只留下所有提到的上传插件的列表:
- 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)
- 约瑟冈萨雷斯/上传
- 英里/上传者
- szajbus/上传包
- webtechnick/CakePHP-FileUpload-Plugin(2.0-branch)
- bmcclure/CakePHP-Media-Plugin(2.0-branch)
- davidpersson/媒体(仅限 1.3.x)
- jrbasso/MeioUpload(已弃用)
Original answer (from 2010):
原始答案(来自 2010 年):
You might consider using the Media pluginthat 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 developmentthan other options I have seen if that's anything to go by (eg. MeioUpload behavior, Uploader plugin, FileUpload pluginand various Bakery/blog posts on the topic).
您可能会考虑使用上次 CakeFest 上讨论过的Media 插件。它相当全面,包含诸如创建文件变体(即图像或视频的缩略图)、输出不同类型文件的助手以及帮助更快创建多文件上传表单的元素等功能;并且从一开始就非常关注安全性,因为我可以从幻灯片中了解到。我在生产中使用它并发现它运行良好,而且它似乎比我见过的其他选项更积极的开发(例如,MeioUpload 行为、Uploader 插件、FileUpload 插件和各种 Bakery/博客文章)话题)。
回答by bancer
Take a look at the File fieldssection in the Cookbook.
回答by rajesh_kw
I found this https://github.com/srs81/CakePHP-AjaxMultiUploadplugin helpful for my case.
我发现这个https://github.com/srs81/CakePHP-AjaxMultiUpload插件对我的案例很有帮助。

