xety/cake3-upload
Composer 安装命令:
composer require xety/cake3-upload
包简介
Cake3 plugin to upload files.
README 文档
README
A Cake3 plugin to upload files.
Requirements
- CakePHP 3.X
Installation
Run : composer require xety/cake3-upload:1.*
Or add it in your composer.json:
"require": { "xety/cake3-upload": "1.*" },
Usage
In your config/bootstrap.php add :
Plugin::load('Xety/Cake3Upload');
In your model initialize():
$this->addBehavior('Xety/Cake3Upload.Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5' ] ] ] );
Set your form to accept files
$this->Form->create($foo, ['type'=>'file']); // .. or .. $this->Form->create($foo, ['enctype' => 'multipart/form-data']);
To create an input to upload a file, just use the this rule : fieldName_file. Example :
<?= $this->Form->input('avatar_file', ['type' => 'file']) ?>
If specified in your Entity, add the suffixed field (e.g. avatar_file) to the $_accessible array:
protected $_accessible = [ # .. 'avatar_field' => true, # .. ];
Identifiers
- :id Id of the Entity (It can be the user Id if you are using this for the users table for example)
- :md5 A random and unique identifier with 32 characters. i.e : bbebb3c3c5e76a46c3dca92c9395ee65
- :y Based on the current year. i.e : 2014
- :m Based on the current month. i.e : 09
Configuration
-
suffix
Default :
_fileYou can change the suffix _file to your own suffix :
$this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5' ] ], 'suffix' => '_anotherName' ] ); <?= $this->Form->input('avatar_anotherName', ['type' => 'file']) ?>
-
overwrite
Default :
trueThis option allow you to define if the behavior must delete and/or overwrite the old file for the field. If the option is false, the file will be not uploaded if the old file name has the same name as the new name file. It can be useful if you want your users to upload only one image.
$this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'overwrite' => false ] ] ] );
-
defaultFile
Default :
falseThis option allow you to defined a default file for the field. It can be useful if you have defined a default avatar for all your new user and you don't want to delete it (i.e : In your database as defaut value for avatar you have set : "../img/default_avatar.png"). Will work only if the overwrite is defined to true.
$this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'overwrite' => true, 'defaultFile' => 'default_avatar.png' ] ] ] );
-
prefix
Default :
falseThis option allow you to defined a prefix for your upload path. Useful if you don't want to use the img/ directory for your upload.
$this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'prefix' => '../' ] ] ] );
Example :
If you use a custom directory at the root of the webroot directory and you use the
HtmlHelperto display your image, you can set a prefix like this :/** * The path will look like this : * webroot/upload/avatar * * In the database, the record will look like that : * ../upload/avatar/1/bbebb3c3c5e76a46c3dca92c9395ee65.png */ $this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'prefix' => '../' ] ] ] ); // In a view, with the Html Helper: <?= $this->Html->image($User->avatar) ?> // Output : <img src="/img/../upload/avatar/1/bbebb3c3c5e76a46c3dca92c9395ee65.png" alt="">
Contribute
xety/cake3-upload 适用场景与选型建议
xety/cake3-upload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.1k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2014 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「file」 「plugin」 「upload」 「cakephp」 「cake3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xety/cake3-upload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xety/cake3-upload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xety/cake3-upload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Kinikit - PHP Application development framework MVC component
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Laravel Media Popup to upload/view the files
PeskyORM - annoying ORM that contains tons of exceptions and throws them when anything goes wrong
统计信息
- 总下载量: 26.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 29
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-02