brenoroosevelt / cakephp-file-db
此包已废弃且不再维护。未建议替代包。
最新版本(1.0.0)的包没有可用的许可信息。
存储文件到数据库 - CakePHP 3.x 插件
1.0.0
2017-03-07 23:54 UTC
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.1 <4.0.0
Requires (Dev)
This package is auto-updated.
Last update: 2021-03-15 22:27:09 UTC
README
安装
我们建议您将其作为composer包安装
"require": { "brenoroosevelt/cakephp-file-db" : "@stable" }
更新包
$ composer update
加载插件
在文件config/bootstrap.php
的末尾添加
Plugin::load('FileDb');
创建表格
使用迁移来生成文件表格
$ bin/cake migrations migrate -p FileDb
此命令将生成以下表格。建议使用迁移。
CREATE TABLE arquivos ( id serial NOT NULL, file_name character varying(255) NOT NULL, file_path character varying(255), file_type character varying(255) NOT NULL, file_size bigint NOT NULL, file_content bytea NOT NULL, model character varying(255) NOT NULL, tag character varying(255) NOT NULL, foreign_key integer NOT NULL, created timestamp without time zone NOT NULL, modified timestamp without time zone, CONSTRAINT attachments_pkey PRIMARY KEY (id) );
如何使用
在其模型中启用行为
创建所需数量的文件配置
$this->addBehavior('FileDb.FileDatabase',[ [ 'alias' => 'Foto', 'type' => 'hasOne', 'form_field' => 'file_foto' // campo usado no formulário ], [ 'alias' => 'Documento', 'type' => 'hasOne', 'form_field' => 'file_documento' ], [ 'alias' => 'Album', 'type' => 'hasMany', // hasMany 'form_field' => 'file_album' ], ]);
在表单中添加字段
<?= $this->Form->create($aluno, ['enctype' => 'multipart/form-data']) ?> <?= $this->Form->file('file_foto'); ?> <?= $this->Form->file('file_documento'); ?> <?= $this->Form->file('file_album.1'); ?> <?= $this->Form->file('file_album.2'); ?> <?= $this->Form->file('file_album.3'); ?> <?= $this->Form->button(__('Submit')) ?> <?= $this->Form->end() ?>
获取文件
在您的控制器中
$aluno = $this->Alunos->get($id, [ 'contain' => ['Foto', 'Album', 'Documento' ] ]);
下载文件
在您的控制器中
public function download($id = null) { // Considere usar um cache antes de fazer a consulta abaixo! $aluno = $this->Alunos->get($id, [ 'contain' => ['Foto'] ]); $file = $aluno->foto->file_content; $this->response->type($aluno->foto->file_type); $this->response->body(function () use ($file) { rewind($file); fpassthru($file); fclose($file); }); $this->response->download($aluno->foto->file_name); // comente para não forçar o download return $this->response; }
删除文件
当关联实体被删除时,删除是自动的。也就是说,如果删除了学生,文件也会被删除。
如果您想删除特定文件或多个文件,请使用以下方法
// file_id: id do arquivo! $this->Alunos->deleteFile($file_id); // id: do aluno, e tag = 'Foto' $this->Alunos->deleteAllFiles($id, $tag=null)
验证
// use bytes(inteiro) ou '1MB' humanizado $validator->add('file_upload', 'file', [ 'rule' => ['fileSize', [ '>', 1024 ]], 'message' =>'Arquivo deve ser maior que 1MB' ]); // limite o tamanho considerandosua regra E: memory_limit, post_max_size e upload_max_filesize $validator->add('file_upload', 'file', [ 'rule' => ['fileSize', [ '<', '2MB' ]], 'message' =>'Arquivo deve ser menor que 2MB' ]); $validator->add('file_upload','create', [ 'rule' => ['extension', ['png', 'pdf']], 'message' =>'Extensão inválida' ]); $validator->add('file_upload','create', [ 'rule' => ['mimeType', ['image/jpeg', 'image/png']], 'message' =>'Tipo inválido', ]); // Erro quando arquivo não pode ser enviado ao servidor, geralmente por causa de: // memory_limit // post_max_size // upload_max_filesize $validator->add('file_upload', 'file', [ 'rule' => ['uploadError'], 'message' =>'Erro ao enviar o arquivo', 'last'=> true ]);
TODO(待完成)
- 磁盘写入选项