upload files

This commit is contained in:
2024-09-10 12:55:41 +03:00
parent 09d8427293
commit fa8fb69f19
7 changed files with 84 additions and 30 deletions

View File

@ -6,16 +6,21 @@ use app\helpers\Debug;
class FileUpload
{
public string $fileTmpPath;
public string $fileName;
public string $fileSize;
public string $fileType;
public array $fileNameCmps;
public string $fileExtension;
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;
// public string $newFileName;
public array $allowedFileExtensions;
public function __construct(array $file, array $extensions)
public function __construct(array $file, array $extensions, $hashing = true)
{
$this->fileTmpPath = $file['tmp_name'];
$this->fileName = $file['name'];
@ -39,17 +44,36 @@ class FileUpload
// return false;
// }
public function upload()
public function upload($uploadDir = "/resources/upload/"): bool
{
$newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
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);
$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;
}
}
public function getFullUploadedPath(): string
{
return $this->destPath;
}
public function getUploadDir(): string
{
return $this->uploadDir;
}
public function getUploadFile(): string
{
return $this->uploadFile;
}
}