diff --git a/kernel/FileUpload.php b/kernel/FileUpload.php index 1f72ec6..a33302e 100644 --- a/kernel/FileUpload.php +++ b/kernel/FileUpload.php @@ -12,10 +12,10 @@ class FileUpload public string $fileType; public array $fileNameCmps; public string $fileExtension; - public string $newFileName; - public array $allowedFileExtensions = ['jpg', 'gif', 'png']; +// public string $newFileName; + public array $allowedFileExtensions; - public function __construct(array $file) + public function __construct(array $file, array $extensions) { $this->fileTmpPath = $file['tmp_name']; $this->fileName = $file['name']; @@ -23,28 +23,32 @@ class FileUpload $this->fileType = $file['type']; $this->fileNameCmps = explode('.', $this->fileName); $this->fileExtension = strtolower(end($this->fileNameCmps)); + $this->allowedFileExtensions = $extensions; } - public function setNewFileName(): void - { - $this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension; - } +// public function setNewFileName(): void +// { +// $this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension; +// } - public function checkExtension(): void - { +// public function checkExtension(): bool +// { +// if (in_array($this->fileExtension, $this->allowedFileExtensions)) { +// return true; +// } +// return false; +// } - } - - public function upload(): void + public function upload() { - $this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension; + $newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension; if (in_array($this->fileExtension, $this->allowedFileExtensions)) { - mkdir('./resources/upload/' . mb_substr($this->newFileName, 0, 2) . '/' . mb_substr($this->newFileName, 2, 2) . '/', 0777, true); - $uploadFileDir = './resources/upload/' . mb_substr($this->newFileName, 0, 2) . '/' . mb_substr($this->newFileName, 2, 2) . '/'; - $dest_path = $uploadFileDir . $this->newFileName; + mkdir('./resources/upload/' . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/', 0777, true); + $uploadFileDir = './resources/upload/' . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/'; + $dest_path = $uploadFileDir . $newFileName; move_uploaded_file($this->fileTmpPath, $dest_path); } else { - echo "Ниченр не получилочь :("; + return false; } } diff --git a/kernel/modules/menu/controllers/MenuController.php b/kernel/modules/menu/controllers/MenuController.php index 8075d3d..2417014 100644 --- a/kernel/modules/menu/controllers/MenuController.php +++ b/kernel/modules/menu/controllers/MenuController.php @@ -34,7 +34,7 @@ class MenuController extends AdminController #[NoReturn] public function actionAdd(): void { if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) { - $file = new FileUpload($_FILES['icon_file']); + $file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png']); $file->upload(); } @@ -97,7 +97,7 @@ class MenuController extends AdminController throw new Exception(message: "The menu item not found"); } if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) { - $file = new FileUpload($_FILES['icon_file']); + $file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png']); $file->upload(); } $menuForm = new CreateMenuForm(); diff --git a/kernel/modules/menu/views/form.php b/kernel/modules/menu/views/form.php index dc35eef..222a1b3 100644 --- a/kernel/modules/menu/views/form.php +++ b/kernel/modules/menu/views/form.php @@ -6,7 +6,7 @@ use kernel\models\Menu; $form = new \itguild\forms\ActiveForm(); -$form->beginForm(isset($model) ? "/admin/settings/menu/edit/" . $model->id : "/admin/settings/menu"); +$form->beginForm(isset($model) ? "/admin/settings/menu/edit/" . $model->id : "/admin/settings/menu", 'multipart/form-data'); $form->field(class: \itguild\forms\inputs\Select::class, name: "parent_id", params: [ 'class' => "form-control", diff --git a/resources/upload/71/bb/71bbe61dded4fb0ba8269540c6989974.png b/resources/upload/71/bb/71bbe61dded4fb0ba8269540c6989974.png deleted file mode 100644 index aa15e6c..0000000 Binary files a/resources/upload/71/bb/71bbe61dded4fb0ba8269540c6989974.png and /dev/null differ diff --git a/resources/upload/5c/40/5c4044ac0df8411aaf0dbdb41846dc29.png b/resources/upload/c7/a6/c7a63f602e0ebb44dff35819ab48436a.png similarity index 100% rename from resources/upload/5c/40/5c4044ac0df8411aaf0dbdb41846dc29.png rename to resources/upload/c7/a6/c7a63f602e0ebb44dff35819ab48436a.png