sandip / cake3.x_file_upload
v1.2.0
2018-07-10 07:58 UTC
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.1.0 <4.0
This package is not auto-updated.
Last update: 2024-09-25 09:40:13 UTC
README
它可以用来上传文件,创建缩略图,删除文件,从图片base64数据创建文件
需求
在master
分支上需要以下要求
- CakePHP 3.0.0或更高版本。
- PHP 5.4.16或更高版本。
安装
- 使用Composer从您的CakePHP项目的根目录(位于
composer.json
文件所在位置)安装插件
composer require sandip/cake3.x_file_upload
Plugin::load('FileUpload');
如何使用
- 在控制器初始化函数中加载插件
namespace App\Controller; use Cake\Event\Event; /** * My Users Controller */ class UsersController extends AppController { public function initialize(){ parent::initialize(); $this->loadComponent('FileUpload.FileUpload',[ 'defaultThumb'=>[ 'small'=>[30,30], 'medium' => [90,90 ] ], 'uploadDir' =>WWW_ROOT.'uploads'.DS.'profile_pic'.DS, 'maintainAspectRation'=>true ]); } }
- 调用一个上传图片的函数
public function add() { $user = $this->Users->newEntity(); if ($this->request->is('post')) { $this->request->data['profile_pic'] = $this->FileUpload->doFileUpload($this->request->data['profile_pic']); $user = $this->Users->patchEntity($user, $this->request->data); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved.')); return $this->redirect(['controller' => 'Users','action' => 'index']); } else { $this->Flash->error(__('The user could not be saved. Please, try again.')); } } $this->set(compact('user')); $this->set('_serialize', ['user']); }