問題描述
誰能建議我在 CakePHP 中編寫用于上傳文件的代碼的最佳方法?
Can any one suggest me the best way to write code for uploading a file in CakePHP?
- 我需要上傳一個文件并將其名稱保存在表格中.
- 如果記錄保存失敗,則不應(yīng)上傳文件.
- 如果上傳失敗,則應(yīng)回滾記錄.
- 代碼應(yīng)該是可重用的
- 我需要在
afterSave
回調(diào)中上傳文件
- 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 當(dāng)前文件插件列表(2.x 分支 這里)
Edit (2015): Please see the Awesome CakePHP list for current file plugins (2.x branch here)
編輯(2013 年):這個答案現(xiàn)在有點(diǎn)過時了,所以我只留下所有提到的上傳插件的列表:
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 上討論過的媒體插件.它相當(dāng)全面,包含諸如創(chuàng)建文件變體(即圖像或視頻的縮略圖)、輸出不同類型文件的助手以及幫助更快創(chuàng)建多文件上傳表單的元素等功能;并且從一開始就非常關(guān)注安全性,因?yàn)槲铱梢詮?幻燈片中收集到.我在生產(chǎn)中使用它并發(fā)現(xiàn)它運(yùn)行良好,而且它似乎處于更多積極開發(fā) 比我見過的其他選項(xiàng)是否有任何意義(例如 MeioUpload 行為、上傳插件、FileUpload 插件 以及有關(guān)該主題的各種 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).
這篇關(guān)于在 CakePHP 中上傳文件的最佳實(shí)踐的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!