お世話になります。
CakePHP2で画像のアップロード掲示板を作成していますが
複数ファイルアップロードについてどうすればできるのか悩んでいます。
※CakePHPの本も複数読んでいますが、複数ファイルをアップロードしたものがありません
--やったこと
まず最初に以下のような投稿フォームを作ろうとしました。
・タイトル(text)
・記事(textarea)
・画像1(file)
・画像2(file)
・画像3(file)
・画像4(file)
そのため、まず下記の2つのテーブルを作成しました。
sql
1CREATE TABLE `posts` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `title` varchar(30) NOT NULL, 4 `description` varchar(150) NOT NULL, 5 `user_id` int(11) NOT NULL, 6 `del_flg` tinyint(1) NOT NULL, 7 `created` datetime NOT NULL, 8 `modified` datetime NOT NULL, 9 PRIMARY KEY (`id`) 10);
sql
1CREATE TABLE `images` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `post_id` int(11) NOT NULL, 4 `image` text NOT NULL, 5 `created` datetime NOT NULL, 6 `modified` datetime NOT NULL, 7 PRIMARY KEY (`id`) 8)
Viewには下記のように書きました。
php
1<?php echo $this->Form->create('Post', array('type' => 'file', 'action' => 'done')); ?> 2<?php echo $this->Form->input('title', array('label' => false, 'type' => 'text')); ?> 3<?php echo $this->Form->input('description', array('label' => false, 'type' => 'textarea')); ?> 4<?php echo $this->Form->file('Post.Image.0.image'); ?> 5<?php echo $this->Form->file('Post.Image.1.image'); ?> 6<?php echo $this->Form->file('Post.Image.2.image'); ?> 7<?php echo $this->Form->file('Post.Image.3.image'); ?> 8<?php echo $this->Form->error('title'); ?> 9<?php echo $this->Form->error('description'); ?> 10<?php echo $this->Form->error('Post.Image.0.image'); ?> 11<?php echo $this->Form->error('Post.Image.1.image'); ?> 12<?php echo $this->Form->error('Post.Image.2.image'); ?> 13<?php echo $this->Form->error('Post.Image.3.image'); ?> 14<?php echo $this->Form->end('投稿する'); ?>
次にPostモデルと、Imageモデルを作りました。
--Post
php
1<?php 2class Post extends AppModel { 3 public $hasMany = array("Image"); 4 public $validate = array( 5 'title' => array( 6 'maxLength' => array( 7 'rule' => array('maxLength', 30), 8 'required' => true, 9 'allowEmpty' => false, 10 ), 11 ), 12 'title' => array( 13 'maxLength' => array( 14 'rule' => array('maxLength', 150), 15 'required' => true, 16 ), 17 ), 18 ); 19} 20?>
-- Image
php
1<?php 2class Image extends AppModel { 3 public $belongsTo = array('Post'); 4 public $validate = array( 5 'image' => array( 6 'allowEmpty' => true, 7 'extension' => array( 8 'rule' => array('extension', array('gif', 'jpeg', 'png', 'jpg')), 9 'message' => '有効な画像ファイルを指定してください。', 10 ), 11 'fileSize' => array( 12 'rule' => array('fileSize', '<=', '2MB'), 13 'message' => '画像は 2MB 未満でなければなりません。' 14 ), 15 'uploadError' => array( 16 'rule' => 'uploadError', 17 'message' => 'ファイルアップロードで障害が起こりました。', 18 ), 19 ), 20 ); 21} 22?>
-- PostsController
php
1<?php 2class PostsController extends AppController { 3 public function done() { 4 $user_id = $this->Auth->user("id"); 5 $this->Post->set('user_id', $user_id); 6 if (!empty($this->request->data['id'])) { 7 // 追加 8 $this->Post->id = $this->request->data['id']; 9 $this->Post->save($this->request->data); 10 }else { 11 // 更新 12 $this->Post->save($this->request->data); 13 $post_id = $this->Post->getLastInsertID(); 14 for ($i = 0; $i < 4; $i++) { 15 if (!$this->Image->save($this->request->data['Image'][$i])) { 16 return $this->render('form'); 17 } 18 } 19 } 20 } 21} 22?>
付け加えると、以下の動作を望んでいます。
・ファイルは最低1つ必要
・指定された場合のファイルサイズは2M以下
・JPEG, GIF, PNGを選択可能。
上記のようにしたいのですが
実際は4つのファイル全てを選択しないと次の画面に進めない状況です。
またwordファイルをアップロードしても次に進めてしまいます。
どこが間違っているのでしょうか。
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。