参考:http://www.kaasan.info/archives/2482
ダウンロード
https://github.com/szajbus/uploadpack
ダウンロードしたファイルを「upload_pack」にリネームして app→Pluginフォルダに入れる。
テーブル作成
[php]
—
— テーブルの構造 `images`
—
CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`img_file_name` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=45 ;
[/php]
Configフォルダのbootstrap.phpにuploadpackを読み込むよ
bootstrap.phpに追記
[php]
CakePlugin::loadAll();
[/php]
Model
Image.php
[php]
class Image extends AppModel {
/**
* Validation rules
*
* @var array
*/
var $name = ‘Image’;
var $actsAs = array(
‘UploadPack.Upload’ => array(
‘img’ => array(
‘styles’ => array(
‘big’ => ‘200×200’,
‘small’ => ‘120×120’,
‘thumb’ => ’80×80′,
)
)
)
);
}
[/php]
イメージ画像が保存されるフォルダの指定がないときは、「webroot→upload→imagesに保存される。
てすので、webrootに”uploadフォルダ”とそのフォルダの中に”imagesフォルダ(勝手に生成されるみたい・・・モデル名だよ)”を作成する。
Controller
ImagesController.php
[php]
class ImagesController extends AppController {
var $uses = array(‘Image’);
var $helpers = array(‘Form’,’UploadPack.Upload’); //ヘルパー読み込み
function index(){
$this->set(‘images’, $this->Image->find(‘all’));
//debug($this->Image->find(‘all’));
}
function show($id) {
$this->set(‘images’, $this->Image->findById($id));
debug($this->Image->findById($id));
}
function add(){
if(!empty($this->data)){
if($this->Image->save($this->data)){
$this->redirect(‘/images’);
}
}
}
function edit($id = null){
$this->Image->id = $id;
if (empty($this->data)){
$this->data = $this->Image->read();
}else{
if ($this->Image->save($this->data[‘Image’])){
$this->redirect(‘/images’);
}
}
}
}
[/php]
View
add.ctp
※bootstrapを使用する場合はここ注意!BootstrapForm->create(‘Image’, array(‘class’ => ‘form-horizontal’,‘type’ => ‘file’));?>
[php]
<?php echo $this->Form->create(‘Image’, array(‘type’ => ‘file’)); ?>
<table>
<tr>
<th>TITLE</th><td><?php echo $this->Form->text(‘Image.title’); ?></td>
</tr>
<tr>
<th>IMAGE</th>
<td>
<?php echo $this->Form->file(‘Image.img’);?>
<?php echo $this->Form->error(‘Image.img’);?></td>
</tr>
<tr>
<th></th>
<td>
<?php echo $this->Form->end(‘新規登録’);?></td>
</tr>
</table>
[/php]
View
index.ctp
[php]
<h2><?php echo $this->Html->link("Add New Image", "/images/add/"); ?></h2>
<table>
<tr>
<th>ID</th><th>TITLE</th><th>IMAGE</th><th>CREATED</th><th>EDIT</th>
</tr>
<?php foreach($images as $image):?><tr>
<td><?php echo $image[‘Image’][‘id’];?></td>
<td><?php echo $image[‘Image’][‘title’];?></td>
<td><?php echo $image[‘Image’][‘img_file_name’];?></td>
<td><?php echo $this->Upload->uploadImage($image, ‘Image.img’, array(‘style’ => ‘thumb’)) ?></td> //モデルで設定した’thumb’を表示させる
<td><?php echo $image[‘Image’][‘created’];?></td>
<td><?php echo $this->Html->link("EDIT", "/edit/".$image[‘Image’][‘id’]); ?>
<?php echo $this->Html->link("show", "/images/show/".$image[‘Image’][‘id’]); ?></td>
</tr>
<?php endforeach;?>
</table>
[/php]
View
show.ctp
[php]
<td><?php echo $this->Upload->uploadImage($images, ‘Image.img’, array(‘style’ => ‘big’)) ?></td> //モデルで設定した’big’を表示させる
[/php]
Upload->uploadImage(… バージョンでヘルパー関数がuploadImageでした。
上手くいきました
備考
イメージが無い時の表示を変える
[php]
‘UploadPack.Upload’ => array(
‘img’ => array(
‘default_url’ => ‘/img/noimage.gif’,
)
)
);
[/php]
バリデーション ファイルサイズとアップロードの種類
[php]
public $validate = array(
‘img’ => array(
‘maxSize’ => array(
‘rule’ => array(‘attachmentMaxSize’, 5000000),
‘message’ => ‘5MB以下のファイルにしてください。’
),
‘attachmentContentType’ => array(
‘rule’ => array(‘attachmentContentType’, array(‘image/jpeg’)),
‘message’ => ‘アップロードできるのはjpegファイルのみです。’
),
)
);
[/php]
View
エラーメッセージの表示欄
[php]
echo $this->BootstrapForm->file(‘Report.img’);
echo ‘<span style="color:#B94A48;" class="help-inline error-message">’ . $this->BootstrapForm->error(‘Report.img’) . ‘</span>’;
[/php]