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