我正在尝试为其中一个模型上传照片以及何时进入编辑模式。当用户只想编辑与该记录相关的文本时,它仍然要求我上传照片。以下是我的验证规则。
'display_photo' => array(
'uploadError' => array(
'rule' => array('uploadError'),
'message' => 'Please select a Photo.',
'allowEmpty' => true,
),
'mimeType' => array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => true,
),
'fileSize' => array(
'rule' => array('fileSize', '<=', '5MB'),
'message' => 'Photo must be less than 5MB.',
'allowEmpty' => true,
),
'photoUpload' => array(
'rule' => array('photoUpload'),
'message' => 'Unable to process Photo upload.',
'allowEmpty' => true,
),
),
上传功能
public function photoUpload($check = array()) {
if (!is_uploaded_file($check['display_photo']['tmp_name'])) {
return false;
}
if (!move_uploaded_file($check['display_photo']['tmp_name'], WWW_ROOT . 'img' . DS .
'portfolios' . DS . $this->data[$this->alias]['slug'].".".pathinfo($check['display_photo']['name'], PATHINFO_EXTENSION))) {
return false;
}
$this->data[$this->alias]['display_photo'] = $this->data[$this->alias]['slug'].".".pathinfo($check['display_photo']['name'], PATHINFO_EXTENSION);
return true;
}