MicroFrameWork/kernel/FileUpload.php

55 lines
1.7 KiB
PHP
Raw Normal View History

2024-09-06 16:53:20 +03:00
<?php
namespace kernel;
use app\helpers\Debug;
class FileUpload
{
public string $fileTmpPath;
public string $fileName;
public string $fileSize;
public string $fileType;
public array $fileNameCmps;
public string $fileExtension;
2024-09-10 11:39:45 +03:00
// public string $newFileName;
public array $allowedFileExtensions;
2024-09-06 16:53:20 +03:00
2024-09-10 11:39:45 +03:00
public function __construct(array $file, array $extensions)
2024-09-06 16:53:20 +03:00
{
$this->fileTmpPath = $file['tmp_name'];
$this->fileName = $file['name'];
$this->fileSize = $file['size'];
$this->fileType = $file['type'];
$this->fileNameCmps = explode('.', $this->fileName);
$this->fileExtension = strtolower(end($this->fileNameCmps));
2024-09-10 11:39:45 +03:00
$this->allowedFileExtensions = $extensions;
2024-09-06 16:53:20 +03:00
}
2024-09-10 11:39:45 +03:00
// public function setNewFileName(): void
// {
// $this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
// }
2024-09-06 16:53:20 +03:00
2024-09-10 11:39:45 +03:00
// public function checkExtension(): bool
// {
// if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
// return true;
// }
// return false;
// }
2024-09-06 16:53:20 +03:00
2024-09-10 11:39:45 +03:00
public function upload()
2024-09-06 16:53:20 +03:00
{
2024-09-10 11:39:45 +03:00
$newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
2024-09-06 16:53:20 +03:00
if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
2024-09-10 11:39:45 +03:00
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;
2024-09-06 16:53:20 +03:00
move_uploaded_file($this->fileTmpPath, $dest_path);
} else {
2024-09-10 11:39:45 +03:00
return false;
2024-09-06 16:53:20 +03:00
}
}
}