2014-10-16 89 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