fusic / filebinder
为 CakePHP 简单的文件附件插件
2.8.0
2019-06-17 12:07 UTC
Requires
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
功能
- 简单设置
- 与 Transition 组件兼容
- 多附件
- 可选的文件存储方法(数据库存储或不)
要求
- PHP >= 5.2.6
- CakePHP >= 2.0
安装
将 'Filebinder' 目录放置在您的 CakePHP 应用程序中的 app/plugins 目录下。然后,在 bootstrap.php 中添加以下代码
<?php
CakePlugin::load('Filebinder');
Filebinder 概述图像
Filebinder 管理 '虚拟表' 和实体。
'简单附件' 模型图像
'多字段' 模型图像
'多模型' 模型图像
实体文件路径图像
用法
添加带有确认页面的图像文件的示例。
<?php
class Post extends AppModel {
public $name = 'Post';
public $actsAs = array('Filebinder.Bindable');
public $displayField = 'title';
public $bindFields = array(array('field' => 'image',
'tmpPath' => '/var/www/html/myapp/app/webroot/files/cache/',
'filePath' => '/var/www/html/myapp/app/webroot/files/',
));
public $validate = array('title' => array('notempty'),
'image' => array('allowExtention' => array('rule' => array('checkExtension', array('jpg')),
'allowEmpty' => true),
'illegalCode' => array('rule' => array('funcCheckFile', 'checkIllegalCode'),
'allowEmpty' => true))
);
/**
* checkIllegalCode
* check include illegal code
*
* @param $filePath
* @return
*/
public function checkIllegalCode($filePath){
$fp = fopen($filePath, "r");
$ofile = fread($fp, filesize($filePath));
fclose($fp);
if (preg_match('/<\\?php./i', $ofile)) {
return false;
}
return true;
}
}
创建附件表。
CREATE TABLE `attachments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`model` text NOT NULL,
`model_id` int(11) NOT NULL,
`field_name` text NOT NULL,
`file_name` text NOT NULL,
`file_content_type` text NOT NULL,
`file_size` int(11) NOT NULL,
`file_object` longtext,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php
class PostsController extends AppController {
public $name = 'Posts';
public $components = array('Session', 'Filebinder.Ring', 'Transition');
/**
* add
*/
public function add() {
$this->Ring->bindUp();
$this->Transition->checkData('add_confirm');
$this->Transition->clearData();
}
/**
* add_confirm
*/
public function add_confirm(){
$this->Transition->checkPrev(array('add'));
$this->Transition->automate('add_success',
false,
'add');
$mergedData = $this->Transition->mergedData();
$this->set('mergedData', $mergedData);
}
/**
* add_success
*/
public function add_success(){
$this->Transition->checkPrev(array('add',
'add_confirm'));
$mergedData = $this->Transition->mergedData();
if ($this->Post->save($mergedData)) {
$this->Transition->clearData();
$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'post'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'post'));
$this->redirect(array('action' => 'add'));
}
}
}
add.ctp
<div class="posts form">
<h2><?php printf(__('Add %s', true), __('Post', true)); ?></h2>
<?php echo $this->Form->create('Post', array('action' => 'add', 'type' => 'file'));?>
<?php echo $this->Form->input('title', array('type' => 'text'));?>
<?php echo $this->Form->input('body');?>
<?php echo $this->Form->input('image', array('type' => 'file'));?>
<?php echo $this->Form->submit(__('Submit', true));?>
<?php echo $this->Form->end();?>
</div>
add_confirm.ctp
<div class="posts form">
<h2><?php printf(__('Confirm %s', true), __('Post', true)); ?></h2>
<?php echo h($mergedData['Post']['title']);?>
<?php echo h($mergedData['Post']['body']);?>
<?php echo $this->Label->image($mergedData['Post']['image']);?>
<?php echo $this->Form->create('Post', array('action' => 'add_confirm'));?>
<?php echo $this->Form->input('dummy', array('type' => 'hidden'));?>
<?php echo $this->Form->submit(__('Submit', true));?>
<?php echo $this->Form->end();?>
</div>
Amazon S3 组合
要求
- aws-sdk 1.5.*
设置
<?php
Configure::write('Filebinder.S3.key', '************************');
Configure::write('Filebinder.S3.secret', '********************************************');
Configure::write('Filebinder.S3.region', 'ap-northeast-1');
<?php
class Post extends AppModel {
public $name = 'Post';
public $actsAs = array('Filebinder.Bindable' => array('storage' => array('Db', 'S3'))); // using Database and Amazon S3 for object storage
public $displayField = 'title';
public $bindFields = array(array(
'field' => 'image',
'tmpPath' => '/var/www/html/myapp/app/webroot/files/cache/',
'filePath' => '/var/www/html/myapp/app/webroot/files/',
'bucket' => 'aws.foobacket', // bucket name,
// 'acl' => AmazonS3::ACL_PUBLIC, // S3 ACL
));
}
许可协议
MIT 许可证
版权所有 (c) 2010-2011 Fusic Co., Ltd. (http://fusic.co.jp)
以下是对任何人免费获得本软件及其相关文档副本("软件")的许可,以便在不限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向提供软件的人提供软件,前提是受以下条件约束:
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
本软件按"现状"提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任(无论基于合同、侵权或其他原因)承担责任,这些索赔、损害或其他责任源于、因之而产生或与此有关软件或软件的使用或其他交易。