bikash / cake_file_upload
上传文件和图片,在 CakePHP 中创建图片缩略图
v1.0
2019-11-19 18:04 UTC
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.6.0 <4.0
This package is not auto-updated.
Last update: 2024-10-03 16:41:20 UTC
README
它可以用来上传文件,创建缩略图,删除文件,从图片 base64 数据创建文件
要求
主分支有以下要求
- CakePHP 3.6.0 或更高版本。
- PHP 5.4.16 或更高版本。
安装
- 使用 Composer 在 CakePHP 项目的根目录(即存放 composer.json 文件的位置)中安装插件
composer require bikash/cake_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']); }