if ($this->request->data('file.size')) {
try {
$fileName = $this->request->data('file.name');
$fileTmp = $this->request->data('file.tmp_name');
$FileApi = new File($fileTmp);
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
$filePath = $fileName.'.'.$fileExt;
$fileDest = FILE_DEST.'/'.$filePath;
// Verify it that allowed extension.
if (!in_array($fileExt, ALLOW_EXTENSIONS)) throw new \Exception('Denied file extensions.');
if (UPLOAD_MAX < $FileApi->size()) throw new \Exception('File size exceeded the upload limit.');
$FileApi->safe();
$FileApi->copy($fileDest, true);
$FileApi->close();
$this->Flash->success('Upload was success.');
} catch (\Exception $e) {
if (!empty($FileApi)) $FileApi->close();
$this->Flash->error($e->getMessage());
}
return $this->redirect($this->referer());
}