From 5b1602f9580c6da2f63cb7bfb5a4209105ad3139 Mon Sep 17 00:00:00 2001 From: fomichev Date: Tue, 11 Feb 2025 17:38:34 +0300 Subject: [PATCH] =?utf8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B0?= =?utf8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/FilesController.php | 30 +++++++++++++++++++++++++-- erp24/services/FileService.php | 16 ++++++++++++++ erp24/views/wiki/_form.php | 2 +- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/erp24/controllers/FilesController.php b/erp24/controllers/FilesController.php index 35b29e5c..2ae4f48e 100755 --- a/erp24/controllers/FilesController.php +++ b/erp24/controllers/FilesController.php @@ -4,14 +4,40 @@ declare(strict_types = 1); namespace app\controllers; +use Yii; +use yii\web\BadRequestHttpException; +use yii\web\Response; +use yii\web\UploadedFile; +use yii_app\services\FileService; + class FilesController extends \yii\web\Controller { public $defaultAction = 'download'; - + public $enableCsrfValidation = false; public function actions() { return [ 'download' => \yii_app\actions\files\DownloadAction::class, ]; } -} \ No newline at end of file + + public function actionUpload() + { + Yii::error(Yii::$app->request->post(), 'upload'); + Yii::error($_FILES, 'upload'); + file_put_contents(Yii::getAlias('@runtime/logs/upload.log'), print_r($_FILES, true), FILE_APPEND); + + $file = UploadedFile::getInstanceByName('upload'); + if (!$file) { + throw new BadRequestHttpException('No file uploaded.'); + } + + $filePath = FileService::saveUploadFileAndReturnUrl($file); + + $fullUrl = Yii::$app->request->hostInfo . '/web' . $filePath; + + file_put_contents(Yii::getAlias('@runtime/logs/upload.log'), print_r($fullUrl, true), FILE_APPEND); + + return $this->asJson(['url' => $fullUrl]); + } +} diff --git a/erp24/services/FileService.php b/erp24/services/FileService.php index 31bee4fd..3ea5a609 100755 --- a/erp24/services/FileService.php +++ b/erp24/services/FileService.php @@ -161,6 +161,22 @@ class FileService return '/' . $targetFile; } + public static function saveUploadFileAndReturnUrl($file) { + $targetDir = Yii::getAlias('@webroot/uploads') . '/' . Yii::$app->user->id . '/' . date("Y") . "/" . date("m") . "/" . date("d"); + + if (!is_dir($targetDir)) { + mkdir($targetDir, 0777, true); + } + + $targetFile = $targetDir . "/" . substr(md5_file($file->tempName), 0, 8) . '_' . $file->baseName . '.' . $file->extension; + $file->saveAs($targetFile); + + // Убираем "/www" из пути + $relativePath = str_replace(Yii::getAlias('@webroot'), '', $targetFile); + + return $relativePath; + } + public static function saveUploadedImage($file, $entity, $entity_id) { $imagePath = \Yii::getAlias('@uploads-images-path'); $target_dir = $imagePath . '/' . Yii::$app->user->id . '/' . date("Y") . "/" . date("m") . "/" . date("d"); diff --git a/erp24/views/wiki/_form.php b/erp24/views/wiki/_form.php index cf965f11..8844bf7a 100644 --- a/erp24/views/wiki/_form.php +++ b/erp24/views/wiki/_form.php @@ -47,7 +47,7 @@ $categoryList = getCategoriesWithHierarchy($categories); field($model, 'content')->widget(CKEditor::class, [ 'options' => ['rows' => 6], - 'uploadUrl' => Yii::getAlias('@uploads') + 'uploadUrl' => '/files/upload' ]) ?> -- 2.39.5