/** * Download file from the link * @param string $url * @param string $path * @param string $name * @param string $extension * @return array|bool * @throws Exception */ public static function saveFromUrl($url, $path, $name = null, $extension = 'png') { if (!FileHelper::createDirectory($path)) { throw new Exception("Directory not created \"{$path}\"."); } $file = file_get_contents(urldecode($url)); $extension = trim($extension, " \t\n\r\v."); if ($name == null) { $name = FileHelper::generateRandomName($path, $extension); } else { $name = $name . '.' . $extension; } if (file_put_contents($path . DIRECTORY_SEPARATOR . $name, $file)) { return ['name' => $name, 'path' => $path]; } return false; }
/** * Сохранение файла на сервере */ public function save() { // Если это действительно экземпляр объекта файла if ($this->file instanceof UploadedFile) { if (!FileHelper::createDirectory($this->_path)) { throw new InvalidParamException("Directory specified in '{$this->_path}' attribute doesn't exist or cannot be created."); } $name = FileHelper::getRandomFileName($this->_path, $this->file->getExtension()); $filePath = $this->_path . DIRECTORY_SEPARATOR . $name; // Если оригинальная картинка сохранилась if ($this->file->saveAs($filePath)) { $imageInfo = getimagesize($filePath); list($width, $height) = $imageInfo; $this->_width = $width; $this->_height = $height; $this->_name = $name; /** @var ImagePathMap $imagePathMap */ $imagePathMap = Yii::$app->get('imagePathMap', false); if ($imagePathMap != null) { $imagePathMap->add($this->_name, $this->_pathID); } return true; } } return false; }
/** * Init variables * @throws \yii\web\HttpException * @throws \yii\web\NotFoundHttpException */ public function afterValidate() { parent::afterValidate(); $this->_pathRootObject = $this->basePath . DIRECTORY_SEPARATOR . $this->folder; $this->_urlRootObject = $this->baseUrl . DIRECTORY_SEPARATOR . $this->folder; if (!is_dir($this->_pathRootObject)) { HttpError::the404(); } $this->_pathObject = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->id; $this->_urlObject = $this->_urlRootObject . DIRECTORY_SEPARATOR . $this->id; if (!is_dir($this->_pathObject) && !FileHelper::createDirectory($this->_pathObject)) { HttpError::the500('Can not create directory: ' . $this->_pathObject); } $this->_pathObjectFile = $this->_pathObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->name; $this->_urlObjectFile = $this->_urlObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->name; $this->_pathObjectFileOrigin = $this->_pathObject . DIRECTORY_SEPARATOR . $this->name; $this->_pathObjectPlaceholder = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder; $this->_urlObjectPlaceholder = $this->_urlRootObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder; $this->_pathObjectPlaceholderOrigin = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->placeholder; $this->_pathRootPlaceholder = $this->basePath . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder; $this->_urlRootPlaceholder = $this->baseUrl . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder; $this->_pathRootPlaceholderOrigin = $this->basePath . DIRECTORY_SEPARATOR . $this->placeholder; }
/** * Обработка заданий на событии "После сохранениея" * @param $event * @return null * @throws ServerErrorHttpException * @throws \yii\base\Exception */ protected function executeAfterSaveTasks($event) { foreach ($this->_afterSaveTaskValue as $fieldName => $fieldTasks) { foreach ($fieldTasks as $fieldTaskName => $fieldTaskValue) { switch ($fieldTaskName) { case 'saveImage': $imageName = $fieldTaskValue; $tempPath = $this->getTempPathByImageName($imageName); $tempImage = $tempPath . DIRECTORY_SEPARATOR . $imageName; $originImage = $this->_basePath . DIRECTORY_SEPARATOR . $imageName; if (file_exists($tempImage)) { if (!FileHelper::createDirectory($this->_basePath)) { throw new InvalidParamException("Directory specified in '{$this->_basePath}' attribute doesn't exist or cannot be created."); } if (!rename($tempImage, $originImage)) { throw new ServerErrorHttpException('Image not renamed: ' . $tempImage . ' to ' . $originImage); } } break; case 'deleteImage': $imageName = $fieldTaskValue; $images = FileHelper::findFiles($this->_basePath, ['only' => ["*{$imageName}*"]]); foreach ($images as $image) { if (!unlink($image)) { throw new ServerErrorHttpException('Image not deleted: ' . $image); } } break; } } } }