This commit is contained in:
Билай Станислав 2024-09-10 11:39:45 +03:00
parent da77807b81
commit fc2e969d77
5 changed files with 24 additions and 20 deletions

View File

@ -12,10 +12,10 @@ class FileUpload
public string $fileType; public string $fileType;
public array $fileNameCmps; public array $fileNameCmps;
public string $fileExtension; public string $fileExtension;
public string $newFileName; // public string $newFileName;
public array $allowedFileExtensions = ['jpg', 'gif', 'png']; public array $allowedFileExtensions;
public function __construct(array $file) public function __construct(array $file, array $extensions)
{ {
$this->fileTmpPath = $file['tmp_name']; $this->fileTmpPath = $file['tmp_name'];
$this->fileName = $file['name']; $this->fileName = $file['name'];
@ -23,28 +23,32 @@ class FileUpload
$this->fileType = $file['type']; $this->fileType = $file['type'];
$this->fileNameCmps = explode('.', $this->fileName); $this->fileNameCmps = explode('.', $this->fileName);
$this->fileExtension = strtolower(end($this->fileNameCmps)); $this->fileExtension = strtolower(end($this->fileNameCmps));
$this->allowedFileExtensions = $extensions;
} }
public function setNewFileName(): void // public function setNewFileName(): void
{ // {
$this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension; // $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()
public function upload(): void
{ {
$this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension; $newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
if (in_array($this->fileExtension, $this->allowedFileExtensions)) { if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
mkdir('./resources/upload/' . mb_substr($this->newFileName, 0, 2) . '/' . mb_substr($this->newFileName, 2, 2) . '/', 0777, true); mkdir('./resources/upload/' . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/', 0777, true);
$uploadFileDir = './resources/upload/' . mb_substr($this->newFileName, 0, 2) . '/' . mb_substr($this->newFileName, 2, 2) . '/'; $uploadFileDir = './resources/upload/' . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/';
$dest_path = $uploadFileDir . $this->newFileName; $dest_path = $uploadFileDir . $newFileName;
move_uploaded_file($this->fileTmpPath, $dest_path); move_uploaded_file($this->fileTmpPath, $dest_path);
} else { } else {
echo "Ниченр не получилочь :("; return false;
} }
} }

View File

@ -34,7 +34,7 @@ class MenuController extends AdminController
#[NoReturn] public function actionAdd(): void #[NoReturn] public function actionAdd(): void
{ {
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) { 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(); $file->upload();
} }
@ -97,7 +97,7 @@ class MenuController extends AdminController
throw new Exception(message: "The menu item not found"); throw new Exception(message: "The menu item not found");
} }
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) { 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(); $file->upload();
} }
$menuForm = new CreateMenuForm(); $menuForm = new CreateMenuForm();

View File

@ -6,7 +6,7 @@
use kernel\models\Menu; use kernel\models\Menu;
$form = new \itguild\forms\ActiveForm(); $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: [ $form->field(class: \itguild\forms\inputs\Select::class, name: "parent_id", params: [
'class' => "form-control", 'class' => "form-control",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB