Example #1
0
 private function setFile($file)
 {
     $fileDir = Yii::$app->controller->module->id . '/' . date('Ymd');
     $path = Yii::$app->params['uploadPath'] . Yii::$app->params['uploadDir'] . '/' . $fileDir;
     $errorCode = $file->error;
     if ($errorCode === UPLOAD_ERR_OK) {
         $type = exif_imagetype($file->tempName);
         if ($type) {
             $extension = image_type_to_extension($type);
             //                $src = $path . '.original' . $extension;
             $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/');
             $fileName = date('YmdHis') . '.original' . $extension;
             $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
             \app\components\FileHelper::createDirectory($folder);
             $src = $folder . $fileName;
             if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG) {
                 if (file_exists($src)) {
                     unlink($src);
                 }
                 $result = $file->saveAs($src);
                 if ($result) {
                     $this->src = $src;
                     $this->type = $type;
                     $this->extension = $extension;
                     $this->setDst($extension);
                 } else {
                     $this->msg = 'Failed to save file';
                 }
             } else {
                 $this->msg = 'Please upload image with the following types: JPG, PNG, GIF';
             }
         } else {
             $this->msg = 'Please upload image file';
         }
     } else {
         $this->msg = $this->codeToMessage($errorCode);
     }
 }