public function run() { // folder for uploaded files $tempFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . $this->tempFolder; if (!is_writable($tempFolder)) { throw new CException('temporary folder is not exists or not writable. Path:' . $tempFolder); } $uploader = new FileUploader($this->allowedFileExtensions, $this->fileLimit); $result = $uploader->handleUpload($tempFolder); if (!isset($result['error'])) { $imageHandler = new CImageHandler(); $imageHandler->load($tempFolder . $result['filename']); try { // if min/max weight/height are set - check those conditions $this->validateImageDimensions($imageHandler->getWidth(), $imageHandler->getHeight()); $this->getController()->successfulAjaxResponse(array('fileName' => $imageHandler->getBaseFileName(), 'imageSrc' => '/application' . $this->tempFolder . $imageHandler->getBaseFileName())); } catch (CException $e) { $errorMsg = $imageHandler->getBaseFileName() . ' - ' . $e->getMessage(); $this->getController()->unsuccessfulAjaxResponse(array('errorMessage' => $errorMsg)); } } else { $this->getController()->unsuccessfulAjaxResponse(array('errorMessage' => $result['error'])); } }
public function beforeSave() { if ($this->fileInstance) { $path = Yii::getPathOfAlias($this->path) . DIRECTORY_SEPARATOR . $this->apartment_id; $name = $this->fileInstance->getName(); $ext = $this->fileInstance->getExtensionName(); while (file_exists($path . DIRECTORY_SEPARATOR . $name)) { $name = rand(0, 9) . $name; } $oldUMask = umask(0); if (!is_dir($path)) { @mkdir($path, 0777, true); } umask($oldUMask); if ($this->fileInstance->saveAs($path . DIRECTORY_SEPARATOR . $name)) { $this->name = $name; if ($ext == 'jpg' || $ext == 'png' || $ext == 'gif') { $image = new CImageHandler(); if ($image->load($path . DIRECTORY_SEPARATOR . $name)) { $this->width = $image->getWidth(); $this->height = $image->getHeight(); } else { return false; } } } else { return false; } } return parent::beforeSave(); }