2014-07-23 78 views
1

我需要驗證用戶上傳縮略圖時的用戶縮略圖大小和類型。但我不知道如何驗證陣列數據。所以,請幫我Cakephp:如何驗證數組

P/S:我使用CakePHP 2.x的

我的數據:

Array 
(
    [User] => Array 

     (
      [id] => 45 
      [username] => pa7264 
      [password] => admin 
      [new_password] => 
      [thumbnail] => Array 
       (
        [name] => demo.jpg 
        [type] => image/jpeg 
        [tmp_name] => D:\OpenServer\userdata\temp\phpD2FD.tmp 
        [error] => 0 
        [size] => 13582 
       ) 

     ) 

) 

回答

0

你需要一個自定義的驗證與和getimagesize()函數的一對。

這是給你一個快速的草案:

//in User.php model 
public $validate = array(
    'thumbnail' => array(
     'imageSize' => array(
      'rule' => array('check_image_size'), 
      'message' => 'Thumbnail size is too big!' 
     ) 
    ) 
); 

// this is custom validaion function 
public function check_image_size($data) 
{ 
    $imagesize = getimagesize($data['thumbnail']['tmp_name']); 
    if (($imagesize[0] > 600) || ($imagesize[1] > 400)){ 

     //here we taking request data to erase image to avoid problems in view 
     $request = Router::getRequest(); 
     unset($request->data['User']['thumbnail']); 
     return false; //validaion failed 
    } 
    return true; //validaion passed 
} 
+0

這裏$ IMAGESIZE [0]是一個寬度和$ IMAGESIZE [1]是一種圖像 – cleverketchup

+1

感謝cleverketchup的高度,我愛你這麼muchhhhhhhh – minhchieng