2024-09-06 16:53:20 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel;
|
|
|
|
|
|
|
|
class FileUpload
|
|
|
|
{
|
2024-09-10 12:55:41 +03:00
|
|
|
protected string $fileTmpPath;
|
|
|
|
|
|
|
|
protected string $destPath;
|
|
|
|
|
|
|
|
protected string $uploadDir;
|
|
|
|
protected string $uploadFile;
|
|
|
|
protected string $fileName;
|
|
|
|
protected string $fileSize;
|
|
|
|
protected string $fileType;
|
|
|
|
protected array $fileNameCmps;
|
|
|
|
protected string $fileExtension;
|
2024-09-10 11:39:45 +03:00
|
|
|
public array $allowedFileExtensions;
|
2024-09-10 15:45:12 +03:00
|
|
|
protected bool $hashing = true;
|
2024-09-06 16:53:20 +03:00
|
|
|
|
2024-09-10 15:45:12 +03:00
|
|
|
public function __construct(array $file, array $extensions, bool $hashing = true)
|
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-10 15:45:12 +03:00
|
|
|
$this->hashing = $hashing;
|
2024-09-06 16:53:20 +03:00
|
|
|
}
|
|
|
|
|
2024-09-10 12:55:41 +03:00
|
|
|
public function upload($uploadDir = "/resources/upload/"): bool
|
2024-09-06 16:53:20 +03:00
|
|
|
{
|
2024-09-10 15:45:12 +03:00
|
|
|
if ($this->hashing) {
|
|
|
|
$newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
|
|
|
|
if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
|
|
|
|
$this->uploadDir = $uploadDir . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/';
|
|
|
|
mkdir(ROOT_DIR . $this->uploadDir, 0777, true);
|
|
|
|
$uploadFileDir = ROOT_DIR . $this->uploadDir;
|
|
|
|
$this->destPath = $uploadFileDir . $newFileName;
|
|
|
|
$this->uploadFile = $this->uploadDir . $newFileName;
|
|
|
|
move_uploaded_file($this->fileTmpPath, $this->destPath);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-06 16:53:20 +03:00
|
|
|
} else {
|
2024-09-10 15:45:12 +03:00
|
|
|
if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
|
|
|
|
$this->uploadDir = $uploadDir;
|
|
|
|
mkdir(ROOT_DIR . $this->uploadDir, 0777, true);
|
|
|
|
$uploadFileDir = ROOT_DIR . $this->uploadDir;
|
|
|
|
$this->destPath = $uploadFileDir . $this->fileName;
|
|
|
|
$this->uploadFile = $this->uploadDir . $this->fileName;
|
|
|
|
move_uploaded_file($this->fileTmpPath, $this->destPath);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-06 16:53:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 15:45:12 +03:00
|
|
|
public function setAllowedFileExtensions(array $allowedFileExtensions): void
|
|
|
|
{
|
|
|
|
$this->allowedFileExtensions = $allowedFileExtensions;
|
|
|
|
}
|
|
|
|
|
2024-09-10 12:55:41 +03:00
|
|
|
public function getFullUploadedPath(): string
|
|
|
|
{
|
|
|
|
return $this->destPath;
|
|
|
|
}
|
|
|
|
|
2024-09-10 15:45:12 +03:00
|
|
|
public function setDestPath(string $destPath): void
|
|
|
|
{
|
|
|
|
$this->destPath = $destPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUploadDir(string $uploadDir): void
|
|
|
|
{
|
|
|
|
$this->uploadDir = $uploadDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUploadFile(string $uploadFile): void
|
|
|
|
{
|
|
|
|
$this->uploadFile = $uploadFile;
|
|
|
|
}
|
|
|
|
|
2024-09-10 12:55:41 +03:00
|
|
|
public function getUploadDir(): string
|
|
|
|
{
|
|
|
|
return $this->uploadDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUploadFile(): string
|
|
|
|
{
|
|
|
|
return $this->uploadFile;
|
|
|
|
}
|
|
|
|
|
2024-09-06 16:53:20 +03:00
|
|
|
}
|