2014-11-14 28 views
-1

我想在cakephp中添加圖像服務器端驗證,但它們不能正常工作任何人都可以幫助我......我是cakephp中的新成員...這是我的視圖和模型文件代碼: - 我使用自定義的插件名稱 「帳戶」 Account.phpcakephp中的圖像驗證無法正常工作

class Account extends AccountAppModel { 
public $useTable = 'users'; 
public $name = "Account"; 
public $validate = array(
'name' => array(
     'rule1' => array(
      'rule' => 'notEmpty', 
      'message' => 'Please enter your name!' 
     ), 
     'alphaNumeric' => array(
      'rule' => 'alphaNumeric', 
      'message' => 'Name must be alphabetic or number!' 
     ), 
     'between' => array(
      'rule' => array('between', 3, 20), 
      'on' => 'create', 
      'message' => 'Name must be between 5to20 character!' 
     ), 
     'isUnique' => array(
      'rule' => 'isUnique', 
      'on' => 'create', 
      'message' => 'Name already taken! please choose another-one!' 
     ) 
    ), 
    'email'=>array(
     'notEmpty'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter Mail ID!' 
     ), 
     'email'=>array(
      'rule'=>'email', 
      'message'=>'Please enter a valid mail id!' 
     ) 
    ), 
    'region'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Please enter Region!' 
    ), 
    'city'=>array(
     'rule'=>'notEmpty', 
     'message'=>'Please enter City!' 
    ), 
    'password'=>array(
     'notEmpty'=>array(
      'rule'=>'notEmpty', 
      'message'=>'please enter password' 
     ), 
     'between'=>array(
      'rule'=>array('between', 5,20), 
      'message'=>'password must be between 5 to 20 character' 
     ) 
    ), 
    'cpassword'=>array(
     'compare' => array(
      'rule'=>array('confirmPassword'),   
      'message' => 'Password do not match please try again!' 
      ) 
    ), 
'photo'=>array(
     'mimeType'=>array(
      'rule'=>array('mimeType',"photo","photo"), 
      'message'=>'Please enter a valid image!' 
     ), 
     'fileSize'=>array(
      'rule' => array('fileSize', '<=', '1MB'), 
      'message'=>'Image is too big please try again!' 
     ), 
     'uploadError'=>array(
      'rule'=>'uploadError', 
      'message'=>'File upload field!' 
     ) 
    ), 
    'photo_id'=>array(
     'mimeType'=>array(
      'rule'=>array('mimeType','photo_id','photo_id'), 
      'message'=>'Please enter a valid image!' 
     ), 
     'fileSize'=>array(
      'rule'=>array('fileSize','<=','1MB'), 
      'message'=>'Image is too big please try again!' 
     ), 
     'uploadError'=>array(
      'rule'=>'uploadError', 
      'message'=>'File upload field!' 
     ) 
    ) 
); 
function confirmPassword() 
{ 
    return $this->data['Account']['password']==$this->data['Account']['cpassword']; 
} 
public function mimeType($field = null,$cfield = null){ 
    //pr($cfield) or die; 
    if(isset($field[$cfield]["tmp_name"]) && !empty($field[$cfield]["tmp_name"])) 
    { 
     $size = @getimagesize($field[$cfield]["tmp_name"]); 
     //pr($size) or die; 
     $imgType = array('image/jpeg','image/jpg'); 
     if(in_array($size['mime'],$imgType)){ 
      $return = true; 
     } 
     else{ 
     $return = false; 
     } 
     return $return; 
    } 
} 
} 

這裏是add.ctp文件代碼: -

<?php echo $this->Form->create('Account',array('type'=>"file")); ?> 

<table width='800px' border='0' align='center'> 
<tr> 
    <td>Name :</td> 
    <td> 
     <?php 
      echo $this->Form->input('name',array('label'=>false,'data-rule-required'=>true,'lettersonly'=>true,"required"=>false)); 
     ?> 
    </td> 
    <td>E-Mail :</td> 
    <td> 
     <?php 
      echo $this->Form->input('email', 
       array('label'=>false,'required'=>false,'type'=>'text')); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>Region :</td> 

    <td> 
     <?php 
      echo $this->Form->input('region', 
       array('type'=>'select', 
        'empty'=>'Select Region', 
        'options'=>$strlt, 
        'label'=>false, 
        'id'=>'name', 
        'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
    <td>City :</td> 
    <td> 
     <?php 
      echo $this->Form->input('city', 
       array('type'=>'select', 
        'empty'=>'Select City', 
        'options'=>'', 
        'label'=>false, 
        'id'=>'slct', 
        'data-rule-required'=>true,'required'=>false 
        ) 
       ); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>Password :</td> 
    <td> 
     <?php 
      echo $this->Form->input('password', 
       array('type'=>'password', 
       'label'=>false, 
       'data-rule-required'=>true, 
       'id'=>'pass_id', 
       'required'=>false)); 
     ?> 
    </td> 
    <td>Confirm Password :</td> 
    <td> 
     <?php 
      echo $this->Form->input('cpassword', 
       array('type'=>'password', 
        'label'=>false, 
        'data-rule-required'=>true, 
        'equalTo'=>'pass_id','required'=>false)); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>Address</td> 
    <td> 
     <?php 
      echo $this->Form->input('address', 
       array('label'=>false, 
         'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
    <td>Zip-Code :</td> 
    <td> 
     <?php 
      echo $this->Form->input('zip', 
       array('label'=>false, 
         'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>About Me :</td> 
    <td> 
     <?php 
      echo $this->Form->textarea('about_me', 
       array('label'=>false, 
         'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
    <td>Contact No :</td> 
    <td> 
     <?php 
      echo $this->Form->input('contact_no', 
       array('label'=>false, 
        'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>Upload Photo :</td> 
    <td> 
     <?php 
      echo $this->Form->input('photo', 
       array('type'=>'file', 
        'label'=>false, 
        'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
    <td>Photo ID :</td> 
    <td> 
     <?php 
      echo $this->Form->input('photo_id', 
       array('type'=>'file', 
         'label'=>false, 
         'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>Show Info :</td> 
    <td> 
     <?php 
      echo $this->Form->input('show_info', 
       array('type'=>'checkbox', 
         'label'=>false)); 
     ?> 
    </td> 
    <td>Premium Member :</td> 
    <td> 
     <?php 
      echo $this->Form->input('membership', 
       array('type'=>'checkbox', 
        'label'=>false,'required'=>false)); 
     ?> 
    </td> 
</tr> 
<tr> 
    <td>Unit/Company :</td> 
    <td> 
     <?php 
      echo $this->Form->input('user_type', array(
         'type' => 'select', 
         'options' => array('unit' => 'Unit', 'company' => 'Company'), 
         'selected' => 'Unit', 
         'label'=>false, 
         'data-rule-required'=>true,'required'=>false 
        )); 
     ?> 
    </td> 
    <td>Status :</td> 
    <td> 
     <?php 
      echo $this->Form->input('status', 
       array('type'=>'select', 
        'options'=>array('1'=>'Active', 
        '0'=>'Inactive'), 
        'label'=>false, 
        'data-rule-required'=>true,'required'=>false)); 
     ?> 
    </td> 
</tr> 
<tr><td colspan=2> 
    <?php echo $this->Form->input('submit', 
     array('type'=>'submit', 
      'label'=>false,'class'=>'btn btn-default','id'=>'sb')); 
    ?> 

    <?php 
     echo $this->Html->link('Cancel', 
     array('plugin'=>'account', 
       'controller'=>'accounts', 
       'action'=>'index'), 
     array('class'=>'btn btn-primary', 
       'div'=>false, 
       'escape'=>false, 
       'id'=>'sb' 
       ) 
      ); 
    ?> 
<td><tr> 
</table> 
<?php echo $this->Form->end();?> 
+0

視頻或文本文件沒有問題..實際上,當我上傳圖像,然後顯示規則消息'請輸入一個有效的圖像!'我不知道錯誤在哪裏! – rahul

回答

1

使用規則,(本替換你的形象規則)

'photo_id'=>array(
    'mimeType'=>array(
    'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')), 
     'message'=>'Please enter a valid image!' 
    ),