Пример #1
0
 /**
  * 找出某一图片文件对应的所有缓存文件
  * Enter description here ...
  * @param unknown_type $fileName
  */
 public static function clearCaches($fileName)
 {
     if ($fileName) {
         $ext = DxdUtil::fileExt($fileName);
         $pattern = substr($fileName, 0, strrpos($fileName, ".")) . "_*." . $ext;
         $pattern = Yii::app()->basePath . "/../caches/" . $pattern;
         $cacheFiles = glob($pattern);
         foreach ($cacheFiles as $file) {
             if (file_exists($file)) {
                 unlink($file);
             }
         }
     }
 }
 /**
  * Uploads file to temporary directory
  *
  * @throws CHttpException
  */
 protected function handleUploading()
 {
     $this->init();
     $model = $this->formModel;
     $model->{$this->fileAttribute} = CUploadedFile::getInstance($model, $this->fileAttribute);
     if ($model->{$this->fileAttribute} !== null) {
         $model->{$this->mimeTypeAttribute} = $model->{$this->fileAttribute}->getType();
         $model->{$this->sizeAttribute} = $model->{$this->fileAttribute}->getSize();
         $model->{$this->displayNameAttribute} = $model->{$this->fileAttribute}->getName();
         $model->{$this->fileNameAttribute} = $model->{$this->displayNameAttribute};
         if ($model->validate()) {
             $path = $this->getPath();
             if (!is_dir($path)) {
                 mkdir($path, 0777, true);
                 chmod($path, 0777);
             }
             //added by liang begine 130815
             $newFilePath = $path . $model->{$this->fileNameAttribute};
             $i = 1;
             while (file_exists($newFilePath) && $i < 10000) {
                 $ext = DxdUtil::fileExt($newFilePath);
                 $newFilePath = substr($newFilePath, 0, strrpos($newFilePath, "."));
                 $newFilePath = $newFilePath . "(" . $i . ")." . $ext;
                 $i++;
             }
             //added by liang end
             //               $model->{$this->fileAttribute}->saveAs($path . $model->{$this->fileNameAttribute});
             $model->{$this->fileAttribute}->saveAs($newFilePath);
             //			chmod($path . $model->{$this->fileNameAttribute}, 0777);
             chmod($newFilePath, 0777);
             $returnValue = $this->beforeReturn();
             if ($returnValue === true) {
                 echo json_encode(array(array("name" => $model->{$this->displayNameAttribute}, "type" => $model->{$this->mimeTypeAttribute}, "size" => $model->{$this->sizeAttribute}, "url" => $this->getFileUrl($model->{$this->fileNameAttribute}), "thumbnail_url" => $model->getThumbnailUrl($this->getPublicPath()), "delete_url" => $this->getController()->createUrl($this->getId(), array("_method" => "delete", "file" => $model->{$this->fileNameAttribute})), "delete_type" => "POST")));
             } else {
                 echo json_encode(array(array("error" => $returnValue)));
                 Yii::log("XUploadAction: " . $returnValue, CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction");
             }
         } else {
             echo json_encode(array(array("error" => $model->getErrors($this->fileAttribute))));
             Yii::log("XUploadAction: " . CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction");
         }
     } else {
         throw new CHttpException(500, "Could not upload file");
     }
 }