CakePHPの超便利なファイルアップロードプラグイン、FileBinderプラグインの使い方をまとめてみた。


CakePHP

MediaPlugin

使FileBinder

FileBinder


FileBinder




DB

1




使

FileBinder






first_name

last_name

age

blood_type

address

email

phone



FileBinder


first_name

last_name

age

blood_type

address

email

phone

profile_image

file_size

file_name

file_content_type

file_object






便

1

Media1


FileBinder


(一)Bindable

(二)

(三)Ring

(四)

(五)bindUp

(六)save







PHP5

CakePHP 1.3

CakePHP1.2

1.2


URLapp/plugins/ 

fusic/filebinder  GitHub

Attachment


Attachment

config/schema.phpCakePHP

file_objectlongtexttext
class AppSchema extends CakeSchema {
    var $name = 'App';

    function before($event = array()) {
        return true;
    }

    function after($event = array()) {
    }

    var $attachments = array(
                           'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
                           'model' => array('type' => 'text', 'null' => false, 'default' => NULL),
                           'model_id' => array('type' => 'integer', 'null' => false, 'default' => NULL),
                           'field_name' => array('type' => 'text', 'null' => false, 'default' => NULL),
                           'file_name' => array('type' => 'text', 'null' => false, 'default' => NULL),
                           'file_content_type' => array('type' => 'text', 'null' => false, 'default' => NULL),
                           'file_size' => array('type' => 'integer', 'null' => false, 'default' => NULL),
                           'file_object' => array('type' => 'text', 'null' => true, 'default' => NULL), // この行のタイプを text に変更
                           'created' => array('type' => 'timestamp', 'null' => true, 'default' => NULL),
                           'modified' => array('type' => 'timestamp', 'null' => true, 'default' => NULL),
                           'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
                           'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
                           );
  }

schema.php

2011.07.26
cake schema create -app your_app_name -plugin filebinder -name app

file_object

text


app/models/attachment.php
class Attachment extends AppModel
{
}

Bindable


Bindable
class Profile extends AppModel
{
 public $actsAs = array(
  'Filebinder.Bindable' => array(
   'model' => 'Attachment', // ファイル情報を保存するモデル名
   'filePath' => WWW_ROOT . 'img' . DS, // ファイルを保存するディレクトリ(絶対パス)
   'dbStorage' => true, // ファイルをバイナリデータとしてデータベースに保存するか
   'beforeAttach' => null, // フック関数(ファイル保存前)
   'afterAttach' => null, // フック関数(ファイル保存後)
   'withObject' => false, // 検索結果にファイルのバイナリデータを付加するか
  )
 );
}


bindFields


$bindFields
class Profile extends AppModel
{
 public $actsAs = array(
  'Filebinder.Bindable' => array(
   'model' => 'Attachment',
   'filePath' => WWW_ROOT . 'img' . DS,
   'dbStorage' => true,
   'beforeAttach' => null,
   'afterAttach' => null,
   'withObject' => false,
  )
 );

 public $bindFields = array(
  array(
   'field' => 'profile_image', // ファイル情報を組み込む仮想フィールド名
   'filePath' => WWW_ROOT . 'profile_image' // ファイルを保存するディレクトリ(絶対パス)。指定されない場合、ビヘイビアの設定が利用されます。
  ),
 );
}


public $bindFields = array(
 array(
  'field' => 'file_upload_filed_name1',
  'filePath' => WWW_ROOT . 'file1'
 ),
 array(
  'field' => 'file_upload_filed_name2'
  'filePath' => WWW_ROOT . 'file2'
 ),
 array(
  'field' => 'file_upload_filed_name3'
 ),
);


Bindable


checkContentType



array(
 'profile_image' => array(
  'rule' => array('checkContentType', array('image/jpeg', 'image/gif', 'image/png'))
 )
);


checkExtension



array(
 'profile_image' => array(
  'rule' => array('checkExtension', array('jpg', 'gif', 'png'))
 )
);


checkFileSize

KBMBGB

array(
 'profile_image' => array(
  'rule' => array('checkFileSize', '10MB')
 )
);


checkMinFileSize

KBMBGB

array(
 'profile_image' => array(
  'rule' => array('checkMinFileSize', '10MB')
 )
);


funcCheckFile



array(
 'profile_image' => array(
  'rule' => array('funcCheckFile', 'userFunction')
 )
);


notEmptyFile



array(
 'profile_image' => array(
  'rule' => array('notEmptyFile')
 )
);




class Profile extends AppModel
{
 public $actsAs = array(
  'Filebinder.Bindable' => array(
   'model' => 'Attachment',
   'filePath' => WWW_ROOT . 'img' . DS,
   'dbStorage' => true,
   'beforeAttach' => null,
   'afterAttach' => null,
   'withObject' => false,
  )
 );

 public $bindFields = array(
  array(
   'field' => 'profile_image',
   'filePath' => WWW_ROOT . 'profile_image'
  ),
 );

 public $validate = array(
  'profile_image' => array(
   'fileSize' => array(
    'rule' => array('checkFileSize', '1MB'),
    'message' => 'ファイルサイズは1MB以内でアップロードしてください。'
   ),
   'fileExt' => array(
    'rule' => array('checkExtension', array('jpg', 'gif', 'png')),
    'message' => '許可されていない拡張子を利用しています。'
   )
  )
 );
}

Ring


Ring
class ProfileController extends AppController
{
 public $components = array(
  'Filebinder.Ring'
 );
}






 2011.07.26
<?php echo $this->Form->create(array('type' => 'file')) ?>
<dl>
<dt>性</dt>
<dd><?php echo $this->Form->text('first_name') ?></dd>
<dt>名</dt>
<dd><?php echo $this->Form->text('last_name') ?></dd>
<dt>年齢</dt>
<dd><?php echo $this->Form->text('age') ?></dd>
<dt>血液型</dt>
<dd><?php echo $this->Form->text('bloodtype') ?></dd>
<dt>住所</dt>
<dd><?php echo $this->Form->text('address') ?></dd>
<dt>メールアドレス</dt>
<dd><?php echo $this->Form->text('email') ?></dd>
<dt>電話番号</dt>
<dd><?php echo $this->Form->text('phone') ?></dd>
<dt>プロフィール画像</dt>
<dd><?php echo $this->Form->file('profile_image') ?></dd>
</dl>
<?php echo $this->Form->end() ?>


BindUp


RingbindUp

bindUpbindUp
class ProfileController extends AppController
{
 public $components = array(
  'Filebinder.Ring'
 );

 public function add()
 {
  if (!empty($this->data)) {
   $this->Ring->bindUp();

   if (!$this->Profile->save()) {
    // 保存完了
   }
  }
 }
}

bindUp

Bindable$modelClass



$profile = $this->Profile->read();
debug($profile);
[Profile] => Array
(
    [id] => 1
    [first_name] => 太郎
    [last_name] => 山田
    [age] => 25
    [blood_type] => A
    [address] => 東京都千代田区
    [email] => taro.yamada@test.com
    [phone] => 03-1234-5678
    [craeted] => 2011-07-20 00:00:00
    [modified] => 2011-07-20 00:00:00
    [profile_image] => Array
        (
            [id] => 1
            [model] => Profile
            [model_id] => 1
            [field_name] => profile_image
            [file_name] => sample.jpg
            [file_content_type] => image/jpeg
            [file_size] => 65536
            [created] => 2011-07-20 00:00:00
            [modified] => 2011-07-20 00:00:00
            [file_path] => /app/webroot/profile_image/Profile/1/profile_image/sample.jpg
            [bindedModel] => Attachment
        )
)



afterFind

CakePHPaftetFind

AhasManyBBFileBinder

AfindBB

便CakePHP

使


Label便
Label


a

img

URL

Label使


HTML使




echo $this->Label->link($profile['profile_image']);


img

echo $this->Label->image($profile['profile_image']);




$this->Form->checkbox('profile_image_delete');

_delete

便


使使便

Media