fix modules and FileUpload class

This commit is contained in:
2024-09-10 15:45:12 +03:00
parent fa8fb69f19
commit 89b6f1ac8c
10 changed files with 139 additions and 112 deletions

View File

@ -17,10 +17,10 @@ class FileUpload
protected string $fileType;
protected array $fileNameCmps;
protected string $fileExtension;
// public string $newFileName;
public array $allowedFileExtensions;
protected bool $hashing = true;
public function __construct(array $file, array $extensions, $hashing = true)
public function __construct(array $file, array $extensions, bool $hashing = true)
{
$this->fileTmpPath = $file['tmp_name'];
$this->fileName = $file['name'];
@ -29,43 +29,65 @@ class FileUpload
$this->fileNameCmps = explode('.', $this->fileName);
$this->fileExtension = strtolower(end($this->fileNameCmps));
$this->allowedFileExtensions = $extensions;
$this->hashing = $hashing;
}
// public function setNewFileName(): void
// {
// $this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
// }
// public function checkExtension(): bool
// {
// if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
// return true;
// }
// return false;
// }
public function upload($uploadDir = "/resources/upload/"): bool
{
$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);
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;
return true;
} else {
return false;
}
} else {
return false;
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;
}
}
}
public function setAllowedFileExtensions(array $allowedFileExtensions): void
{
$this->allowedFileExtensions = $allowedFileExtensions;
}
public function getFullUploadedPath(): string
{
return $this->destPath;
}
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;
}
public function getUploadDir(): string
{
return $this->uploadDir;