add modules and upload file
This commit is contained in:
51
kernel/FileUpload.php
Normal file
51
kernel/FileUpload.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?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;
|
||||
public string $newFileName;
|
||||
public array $allowedFileExtensions = ['jpg', 'gif', 'png'];
|
||||
|
||||
public function __construct(array $file)
|
||||
{
|
||||
$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));
|
||||
}
|
||||
|
||||
public function setNewFileName(): void
|
||||
{
|
||||
$this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
|
||||
}
|
||||
|
||||
public function checkExtension(): void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function upload(): void
|
||||
{
|
||||
$this->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;
|
||||
move_uploaded_file($this->fileTmpPath, $dest_path);
|
||||
} else {
|
||||
echo "Ниченр не получилочь :(";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user