2014-10-16 93 views
-1

CakePHP 2.x:我很努力驗證表單中的圖像上傳字段以添加用戶。上傳字段不是強制性的。但它在圖像上傳時始終將該字段驗證爲false。似乎整個驗證工作部分工作。任何幫助,將不勝感激CakePHP:如果文件不存在,表單上傳字段的驗證

user.php的模型:

public $validate = array(
    'picture' => array(
     'required' => false, 
     'allowEmpty' => true, 
     'custom' => array(
      'rule' => array('imageExist'), 
      'message' => 'There is already a picture with that name on the server' 
     ) 
    )); 


// function to check if file already exists 
public function imageExist($check) { 
    $picturename = $check['picture']; 
    $path = WWW_ROOT . 'userimages/'; 
    $file = $path . $picturename; 

    if (file_exists($file)) { 
     return false; 
    } else { 
     return true; 
    } 
} 

add.ctp:

<?php 
echo $this->Form->create('User', array('class' => 'form-horizontal', 'role' => 'form', 'div' => false, 'type' => 'file')); 
echo $this->Form->input('username', array('label' => "Username")); 
echo $this->Form->input('picture', array('label' => "Avatar", 'type' => 'file')); 
echo $this->Form>formDefaultActions(); 
echo $this->Form->end(); 
?> 

UserController.php:

public function add() { 
    if ($this->request->is('post')) { 
     $this->User->create(); 
     // set picture and path 
     $filedir = WWW_ROOT . 'userimages/'; 
     $file = $filedir . $this->request->data['User']['picture']['name']; 

     // upload avatar picture 
     move_uploaded_file(
      $this->request->data['User']['picture']['tmp_name'], 
      $file 
     ); 
     $this->request->data['User']['picture'] = $this->request->data['User']['picture']['name']; 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been added'), 'success'); 
      $this->redirect(array(
       'action' => 'index' 
      )); 
     } else { 
      $this->Session->setFlash(__('The user could not be created. Please, try again'), 'error'); 
     } 
    } 
} 
+0

現在是什麼問題呢?笏不工作?我們的代碼非常棒。 – 2014-10-16 14:59:38

+0

它總是返回false,即使使用false,數據也不會寫入數據庫,但圖像上傳 – 2014-10-16 15:10:18

+0

您檢查圖像是否已經存在於服務器上的FOLDER上。它不應該重複好嗎? – 2014-10-16 15:54:26

回答

1

您需要檢查您的驗證上傳或將永遠是虛假的。如果你上傳你的文件,當cakephp驗證你的文件已經存在於文件夾中時。

您只需將您的邏輯是這樣的:

$this->request->data['User']['picture'] = $this->request->data['User']['picture']['name']; 
if ($this->User->save($this->request->data)) { 
    move_uploaded_file(
     $this->request->data['User']['picture']['tmp_name'], 
     $file 
    ); 
} 

或檢查,在保存前:

if ($this->User->validates()) { 
    if ($this->User->save($this->request->data)) { 
     move_uploaded_file(
      $this->request->data['User']['picture']['tmp_name'], 
      $file 
     ); 
    } 
} 
+0

正確,Bcoz如果驗證成功, - >然後只保存圖像名稱在數據庫中,然後上傳文件。 +1 :) – 2014-10-17 03:58:04

-1

評論這

$this->request->data['User']['picture'] = $this->request->data['User']['picture']['name']; 

,並脫下2個語句進入模型,需要並允許Empty.also替換您的控制器:

if ($this->User->save($this->request->data)) 

if ($this->User->save($this->request->data['User'])) 

和PIC的名字應該被保存到數據庫

+0

OP的代碼是正確的,'$ this-> User-> save($ this-> request-> data))'是完全有效的,我在每個項目中都使用 – 2014-10-17 03:57:07

+0

這是我的習慣,解決方案有效 – frankiehf 2014-10-17 07:04:01