/**
  * 保存一个附件
  * @param unknown_type $attribute
  * @throws CException
  */
 public function saveAttachment($attribute)
 {
     $this->timestamp = time();
     $file = AttachmentUploadedFile::getInstance($this->Owner, $attribute);
     if (!is_null($file)) {
         //		if(!$this->Owner->isNewRecord){
         //delete previous attachment
         $this->Owner->refresh();
         if (file_exists($this->Owner->{$attribute})) {
             unlink($this->Owner->{$attribute});
         }
         $this->Owner->isNewRecord = false;
         //		}else{
         //				$this->Owner->isNewRecord = false;
         //		}
         preg_match('/\\.(.*)$/', $file->name, $matches);
         $this->file_extension = CFileHelper::getExtension($file->name);
         //	$this->file_extension = end($matches);
         //检查后缀名是否符合
         if (!empty($this->items[$attribute]['exts']) && !in_array(strtolower($this->file_extension), $this->items[$attribute]['exts'])) {
             return false;
         }
         $this->filename = $file->name;
         $path = $this->getParsedPath($attribute);
         preg_match('|^(.*[\\\\/])|', $path, $match);
         $folder = end($match);
         if (!is_dir($folder)) {
             mkdir($folder, 0777, true);
         }
         $file->saveAs($path, false);
         $file_type = filetype($path);
         $this->Owner->saveAttributes(array($attribute => $path));
         /*			$attributes = $this->Owner->attributes;
         
         			if(array_key_exists('file_size', $attributes)){
         				$this->Owner->saveAttributes(array('file_size' => filesize($path)));
         			}
         			if(array_key_exists('file_type', $attributes)){
         				$this->Owner->saveAttributes(array('file_type' => mime_content_type($path)));
         			}
         			if(array_key_exists('extension', $attributes)){
         				$this->Owner->saveAttributes(array('extension' => $this->file_extension));
         			}*/
         if (isset($this->Owner->{$attribute . "Size"})) {
             $this->Owner->saveAttributes(array($attribute . "Size" => filesize($path)));
         }
         if (isset($this->Owner->{$attribute . "Type"})) {
             $this->Owner->saveAttributes(array($attribute . "Type" => mime_content_type($path)));
         }
         if (isset($this->Owner->{$attribute . "Ext"})) {
             $this->Owner->saveAttributes(array($attribute . "Ext" => $this->file_extension));
         }
         if (isset($this->Owner->{$attribute . "Extension"})) {
             $this->Owner->saveAttributes(array($attribute . "Extension" => $this->file_extension));
         }
         #processors
         if (!empty($this->processors)) {
             foreach ($this->processors as $processor) {
                 $p = new $processor['class']($path);
                 $p->output_path = $path;
                 $p->{$processor['method']}($processor['params']);
             }
         }
         /**
          * process resize if we have multiple sizes
          */
         if (!empty($this->styles)) {
             $this->path = str_replace('.:ext', '-:custom.:ext', $this->path);
             if (class_exists('Imagick', false)) {
                 $processor = new ImagickProcessor($path);
             } else {
                 if (!function_exists("gd_info")) {
                     throw new CException('GD or Imagick extension needs to image resize.');
                 }
                 $processor = new GDProcessor($path);
             }
             // if the dimensions start with an ! the keepratio will be false
             foreach ($this->styles as $style => $size) {
                 $processor->output_path = $this->getParsedPath($attribute, $style);
                 $s = explode('x', $size);
                 if ($s[0][0] == '!') {
                     $s[0] = ltrim($s[0], '!');
                     $keepratio = false;
                 } else {
                     $keepratio = true;
                 }
                 $processor->resize(array('width' => $s[0], 'height' => $s[1], 'keepratio' => $keepratio));
             }
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * 保存一个附件
  * @param unknown_type $attribute
  * @throws CException
  */
 public function saveAttachment($attribute)
 {
     $file = AttachmentUploadedFile::getInstance($this->Owner, $attribute);
     if (!is_null($file)) {
         //			if(!$this->Owner->isNewRecord){
         //delete previous attachment
         //			if(file_exists($this->Owner->{$attribute})){
         $this->Owner->refresh();
         $uploadFile = UploadFile::model()->findByPk($this->Owner->{$attribute});
         if ($uploadFile) {
             $uploadFile->delete();
         }
         $this->Owner->isNewRecord = false;
         //unlink($this->Owner->{$attribute});
         //		}
         //			}else{
         //				$this->Owner->isNewRecord = false;
         //			}
         preg_match('/\\.(.*)$/', $file->name, $matches);
         $this->file_extension = end($matches);
         //检查后缀名是否符合
         if (!empty($this->items[$attribute]['exts']) && !in_array($this->file_extension, $this->items[$attribute]['exts'])) {
             return false;
         }
         $this->filename = $file->name;
         $path = $this->getParsedPath($attribute);
         preg_match('|^(.*[\\\\/])|', $path, $match);
         $folder = end($match);
         if (!is_dir($folder)) {
             mkdir($folder, 0777, true);
         }
         $file->saveAs($path, false);
         $file_type = filetype($path);
         $uploadFile = new UploadFile();
         $uploadFile->userId = Yii::app()->user->id;
         $uploadFile->addTime = time();
         $uploadFile->name = $file->name;
         //$uploadFile->mime  = mime_content_type($path);
         $uploadFile->mime = CFileHelper::getMimeType($path) ? CFileHelper::getMimeType($path) : "";
         $uploadFile->type = $this->file_extension;
         $uploadFile->size = filesize($path);
         $uploadFile->path = $path;
         $uploadFile->save();
         $this->Owner->saveAttributes(array($attribute => $uploadFile->id));
     }
     return true;
 }