###前提・実現したいこと
CakePHPのフォームヘルパーを使って、チュートリアル通りに作った
掲示板(ブログ)に画像を投稿したいのですが、
投稿された画面には画像が画像として表示されず、
「hoge.jpg」のようにファイル名として表示されてしまいます。
以下、add.ctp
<!-- File: /app/View/Posts/add.ctp --> <h1>Add Post</h1> <?phpecho $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('type' => 'file',',method' => 'post','enctype' => 'multipart/form-data'));
echo $this->Form->end('Save Post');
?>
以下、view.ctp
<!-- File: /app/View/Posts/view.ctp --> <h1><?php echo h($post['Post']['title']); ?></h1> <p><small>Created: <?php echo $post['Post']['created']; ?></small></p> <p><?php echo h($post['Post']['body']); ?></p>以下、PostsController.php
<?php class PostsController extends AppController { public function fileup(){ if ($this->request->is('post') || $this->request->is('put')) { //画像の保存 if($this->Post->save($this->request->data)){ //画像保存先のパス $path = ('img/'); $image = $this->request->data['body']['image']; $this->Session->setFlash('画像を登録しました'); move_uploaded_file($image['tmp_name'], $path . DS . $image['name']); }else{ $this->Session->setFlash('画像が登録できませんでした'); } } } public $helpers = array('Html', 'Form', 'Flash'); public $components = array('Flash'); public function index() { $this->set('posts', $this->Post->find('all')); } public function view($id) { if (!$id) { throw new NotFoundException(__('Invalid post')); } $post = $this->Post->findById($id); if (!$post) { throw new NotFoundException(__('Invalid post')); } $this->set('post', $post); } public function add() { if ($this->request->is('post')) { //Added this line $this->request->data['Post']['user_id'] = $this->Auth->user('id'); if ($this->Post->save($this->request->data)) { $this->Flash->success(__('Your post has been saved.')); return $this->redirect(array('action' => 'index')); } } } // public function isAuthorized($user) { // 登録済ユーザーは投稿できる if ($this->action === 'add') { return true; } // 投稿のオーナーは編集や削除ができる if (in_array($this->action, array('edit', 'delete'))) { $postId = (int) $this->request->params['pass'][0]; if ($this->Post->isOwnedBy($postId, $user['id'])) { return true; } } return parent::isAuthorized($user); } // public function edit($id = null) { if (!$id) { throw new NotFoundException(__('Invalid post')); } $post = $this->Post->findById($id); if (!$post) { throw new NotFoundException(__('Invalid post')); } if ($this->request->is(array('post', 'put'))) { $this->Post->id = $id; if ($this->Post->save($this->request->data)) { $this->Flash->success(__('Your post has been updated.')); return $this->redirect(array('action' => 'index')); } $this->Flash->error(__('Unable to update your post.')); } if (!$this->request->data) { $this->request->data = $post; } } // public function delete($id) { if ($this->request->is('get')) { throw new MethodNotAllowedException(); } if ($this->Post->delete($id)) { $this->Flash->success( __('The post with id: %s has been deleted.', h($id)) ); } else { $this->Flash->error( __('The post with id: %s could not be deleted.', h($id)) ); } return $this->redirect(array('action' => 'index')); } // } ?>