public function afterValidate($event) { $this->prepareDataDirectory(); $file = CUploadedFile::getInstanceByName($this->uploadInstance); if ($file instanceof CUploadedFile && $file->getError() == UPLOAD_ERR_OK && !$this->Owner->hasErrors()) { $uniqueFilename = P3StringHelper::generateUniqueFilename($file->getName()); $fullFilePath = $this->_fullDataPath . DIRECTORY_SEPARATOR . $uniqueFilename; $relativeFilePath = $this->_relativeDataPath . DIRECTORY_SEPARATOR . $uniqueFilename; if ($file->saveAs($fullFilePath)) { #echo $fullFilePath;exit; if (!$this->Owner->isNewRecord) { $this->deleteFile($this->Owner->path); } if (!$this->Owner->title) { $this->Owner->title = P3StringHelper::cleanName($file->name, 32); } $this->Owner->path = $relativeFilePath; $this->Owner->mimeType = $file->type; $this->Owner->size = $file->size; $this->Owner->originalName = $file->name; $this->Owner->md5 = md5_file($fullFilePath); } else { $this->Owner->addError('filePath', 'File uploaded failed!'); } } else { if ($this->Owner->isNewRecord) { #$this->Owner->addError('filePath', 'No file uploaded!'); Yii::trace('No file uploaded!'); } } }
private function createMedia($fileName, $filePath) { $fullFilePath = Yii::getPathOfAlias($this->module->dataAlias) . DIRECTORY_SEPARATOR . $filePath; $md5 = md5_file($fullFilePath); $getimagesize = getimagesize($fullFilePath); $model = new P3Media(); $model->detachBehavior('Upload'); $model->title = P3StringHelper::cleanName($fileName, 32); $model->originalName = $fileName; $model->type = 1; //P3Media::TYPE_FILE; $model->path = $filePath; $model->md5 = $md5; if (!($mime = mime_content_type($fullFilePath))) { $mime = $getimagesize['mime']; } $model->mimeType = $mime; $model->info = CJSON::encode(getimagesize($fullFilePath)); $model->size = filesize($fullFilePath); if ($model->save()) { return $model->attributes; } else { $errorMessage = ""; foreach ($model->errors as $attrErrors) { $errorMessage .= implode(',', $attrErrors); } throw new CHttpException(500, $errorMessage); } }