public function afterSave(Model $model, $created, $options = array()) { foreach ($this->options[$model->alias]['fields'] as $field => $path) { if (isset($model->data[$model->alias][$field . '_file'])) { $file = $model->data[$model->alias][$field . '_file']; $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if (!empty($file['tmp_name'])) { //debug($path);die(); $path = $model->getUploadPath($path, $extension); // Let's remove the previously uploaded file //$old_extension = $model->field('avatar'); //$old_file = WWW_ROOT . 'assets'. DS .'users'. DS .'profiles'. DS . $model->id . '.' . $old_extension; //if( file_exists($old_file)) unlink($old_file); // Create the folders if necessary $dirname = dirname($path); if (!file_exists(WWW_ROOT . $dirname)) { mkdir(WWW_ROOT . $dirname, 0777, true); } $model->deleteOldUpload($field); // Move the new file move_uploaded_file($file['tmp_name'], WWW_ROOT . $path); // Ensuring the file will accessible via FTP on certain servers chmod(WWW_ROOT . $path, 0777); $model->savefield('avatar', $path); } } } }