2013-12-10 38 views
0

我已經用於上傳圖像的成分,有在控制器沒有問題之後添加component.Here代碼圖像不上傳cakephp的2.0

class OesUsersController extends AppController { 

var $helpers = array('Html', 'Form'); 
var $components = array('upload'); 

public function index() { 
} 

public function upload() 
{ 

    if (empty($this->data)) 
    { 
     $this->render(); 
    } 
    else 
    { 
     $this->cleanUpFields(); 

     // set the upload destination folder 
     $destination = realpath('../../app/webroot/img/uploads/') . '/'; 

     // grab the file 
     $file = $this->data['Image']['filedata']; 

     // upload the image using the upload component 
     $result = $this->Upload->upload($file, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg')); 

     if (!$result){ 
      $this->data['Image']['filedata'] = $this->Upload->result; 
     } else { 
      // display error 
      $errors = $this->Upload->errors; 

      // piece together errors 
      if(is_array($errors)){ $errors = implode("<br />",$errors); } 

      $this->Session->setFlash($errors); 
      $this->redirect('/images/upload'); 
      exit(); 
     } 
     if ($this->Image->save($this->data)) { 
      $this->Session->setFlash('Image has been added.'); 
      $this->redirect('/images/index'); 
     } else { 
      $this->Session->setFlash('Please correct errors below.'); 
      unlink($destination.$this->Upload->result); 
     } 
    } 
} 

的問題是圖像並非來自ADD。 CTP

這裏add.ctp代碼

<label for="Image">Image:</label> 
<input type="file" name="data[Image][filedata]" id="ImageFiledata" /> 

add函數代碼

public function add() { 
    $clint_ip=$this->request->clientIp(); 
    if ($this->request->is('post')) { 

     $this->OesUser->create(); 

     pr($this->request->data); 
     $this->request->data['OesUser']['user_otpkey']=String::uuid(); 
     $this->request->data['OesUser']['user_regdate']=date("Y-m-d H:i:s"); 
     $this->request->data['OesUser']['user_ip']=$clint_ip; 
     $this->barcode($this->request->data['OesUser']['user_otpkey']); 
     if ($this->OesUser->save($this->request->data)) { 
      $this->Session->setFlash(__('The oes user has been saved'), 'flash_success'); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The oes user could not be saved. Please, try again.'), 'flash_fail'); 
     } 
    } 
    $this->set('ip',$clint_ip); 
} 

這裏,數據庫字段名稱:圖像 控制器名稱:OesUsers 型號名稱:OesUser

對我採取的幫助從這個鏈接 http://labs.iamkoa.net/2007/10/23/image-upload-component-cakephp/

+0

你有沒有檢查過,如果你提交它會去上傳功能?也許你錯誤地設置了表單。這裏有一些你可以閱讀的資源。 http://stackoverflow.com/questions/16262194/file-upload-in-cakephp-2-3 – iCezz

回答

0

你的整個造型是怎麼看起來像加滿工作。 CTP?

這聽起來對我說,你沒加

enctype="multipart/form-data" 

的形式。這將導致表單不發佈文件。

而且,建議使用Form幫助器來創建表單。 使用表單助手時,指定要歸檔的表單類型

$this->Form->create('model',array('type'=>'file'));