2014-03-07 3 views
0

사진을 업로드하는 데이 사진을 사용하고 싶지만 사진의 이름과 문자열 만 DB에 저장하고 싶습니다. 사진을 내 폴더에 업로드하기 위해 버그를 수정하려면 어떻게해야합니까?MilesJohnson 업 로더를 사용하는 Cakephp2.2.5는 사진을 업로드 할 수 없습니다.

내 컨트롤러, 모델, 여기에 코드를 붙여 넣습니다. INT STR : 텍스트 IMG : 테이블 : ID 이미지

내 테이블 스키마

은 VARCHAR

감사

<?php 
App::uses('AppController', 'Controller'); 
App::import('Vendor', 'Uploader.Uploader'); 
CakePlugin::load('Uploader'); 
class ImagesController extends AppController { 
public $components = array('Paginator'); 

public function index() { 
    $this->Image->recursive = 0; 
    $this->set('images', $this->Paginator->paginate()); 
} 

public function view($id = null) { 
    if (!$this->Image->exists($id)) { 
     throw new NotFoundException(__('Invalid image')); 
    } 
    $options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id)); 
    $this->set('image', $this->Image->find('first', $options)); 
} 
public function add() { 
    if ($this->request->is('post')) { 
     $this->Image->create(); 
     if ($this->Image->save($this->request->data)) { 
      $this->Session->setFlash(__('The image has been saved.')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The image could not be saved. Please, try again.')); 
     } 
    } 
} 

public function edit($id = null) { 
    if (!$this->Image->exists($id)) { 
     throw new NotFoundException(__('Invalid image')); 
    } 
    if ($this->request->is(array('post', 'put'))) { 
     if ($this->Image->save($this->request->data)) { 
      $this->Session->setFlash(__('The image has been saved.')); 
      return $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The image could not be saved. Please, try again.')); 
     } 
    } else { 
     $options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id)); 
     $this->request->data = $this->Image->find('first', $options); 
    } 
} 

public function delete($id = null) { 
    $this->Image->id = $id; 
    if (!$this->Image->exists()) { 
     throw new NotFoundException(__('Invalid image')); 
    } 
    $this->request->onlyAllow('post', 'delete'); 
    if ($this->Image->delete()) { 
     $this->Session->setFlash(__('The image has been deleted.')); 
    } else { 
     $this->Session->setFlash(__('The image could not be deleted. Please, try again.')); 
    } 
    return $this->redirect(array('action' => 'index')); 
}} 

?> 

다음과 같은 모델입니다

<?php 
App::uses('AppModel', 'Model'); 
App::uses('AttachmentBehavior', 'Uploader.Model/Behavior'); 
CakePlugin::load('Uploader'); 
class Image extends AppModel { 
//from Polin 
public $name = 'Image'; 
public $actsAs = array(
    'Uploader.Attachment' => array(
     'image' => array(
      'nameCallback' => 'formatName', 
      'tempDir' => TMP, 
      'uploadDir' => 'C:\\wamp\\www\\huahei224\\app\\webroot\\img\\uploads', 
      'overwrite' => false, 
      'stopSave' => true, 
      'allowEmpty' => true, 
      'transforms' => array(
       'image_md' => array(
        'nameCallback' => 'transformNameCallback', 
        'class' => 'resize', 
        'append' => '-md', 
        'overwrite' => true, 
        'width' => 400, 
        'height' => 300, 
        'aspect' => true 
       ), 
       'image_sm' => array(
        'nameCallback' => 'transformNameCallback', 
        'class' => 'crop', 
        'append' => '-sm', 
        'overwrite' => true, 
        'self' => false, 
        'width' => 150, 
        'height' => 150 
       ) 
      ) 
     ) 
    ), 
    /** 'Uploader.FileValidation' => array(
     'image' => array(
      'minHeight' => 164, 
      'extension' => array('gif', 'jpg', 'png', 'jpeg'), 
      'type' => 'image', 
      'filesize' => 5242880, 
      'required' => false 
     ) 
    ) 
    **/ 
); 
public function formatName($name, $file) { 
    return sprintf('%s', uniqid()); 
} 
public function transformNameCallback($name, $file) { 
    return $this->getUploadedFile()->name(); 
} 
public $validate = array(
    'img' => array(
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      //'message' => 'Your custom message here', 
      //'allowEmpty' => false, 
      //'required' => false, 
      //'last' => false, // Stop validation after this rule 
      //'on' => 'create', // Limit validation to 'create' or 'update' operations 
     ), 
    ), 

    'str' => array(
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      //'message' => 'Your custom message here', 
      //'allowEmpty' => false, 
      //'required' => false, 
      //'last' => false, // Stop validation after this rule 
      //'on' => 'create', // Limit validation to 'create' or 'update' operations 
     ), 
    ), 
); 
} 

마지막 하나는 add.ctp입니다.

01 23,516,
<div class="images form"> 
<?php echo $this->Form->create('Image'); ?> 
<fieldset> 
    <legend><?php echo __('Add Image'); ?></legend> 
<?php 
    //echo $this->Form->input('img'); 
    echo $this->Form->create('Image', array('type' => 'file')); 
    echo $this->Form->input('str',array('label' => '文字說明')); 
?> 

<?php 
    echo $this->Form->input('img', array('type' => 'file','label' => 'upload photos')); 
    //echo $this->Form->end('Submit'); 
?> 
</fieldset> 
<?php echo $this->Form->end(__('Submit')); ?> 
</div> 
<div class="actions"> 
<h3><?php echo __('Actions'); ?></h3> 
<ul> 

    <li><?php echo $this->Html->link(__('List Images'), array('action' => 'index')); ?></li> 
</ul> 

답변

0

시도는 다음과 같은 추가 :

이가 업로드를 포함하는 형태 필요
<?php echo $this->Form->create('Image', array('enctype' => 'multipart/form-data')); ?> 

, 참조 : FormHelper File