@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Core\Db;
|
||||
|
||||
/**
|
||||
* Единая функция для заливки файлов на сервер.
|
||||
*
|
||||
@@ -15,21 +17,29 @@ class Uploader
|
||||
private string $savePath;
|
||||
private string $extensions = 'jpg|png|jpeg|gif';
|
||||
private array $extMatches = [];
|
||||
private array $FILE;
|
||||
private $cnm;
|
||||
private array $file;
|
||||
private string $customName;
|
||||
|
||||
public static string $error;
|
||||
|
||||
public function __construct($name, $cnm = null)
|
||||
/**
|
||||
* @param string $customName
|
||||
*/
|
||||
public function setCustomName(string $customName): void
|
||||
{
|
||||
$this->customName = $customName;
|
||||
}
|
||||
|
||||
public function __construct($name)
|
||||
{
|
||||
if (!isset($_FILES[$name])) {
|
||||
return;
|
||||
}
|
||||
$this->FILE = $_FILES[$name];
|
||||
$this->cnm = $cnm;
|
||||
$this->file = $_FILES[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
* @param int $max
|
||||
* @param int|null $min
|
||||
* @return void
|
||||
*/
|
||||
@@ -40,7 +50,7 @@ class Uploader
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
* @param int $max
|
||||
* @param int|null $min
|
||||
* @return void
|
||||
*/
|
||||
@@ -98,7 +108,7 @@ class Uploader
|
||||
|
||||
private function hasNormalDimensions(): bool
|
||||
{
|
||||
[$width, $height] = getimagesize($this->FILE['tmp_name']);
|
||||
[$width, $height] = getimagesize($this->file['tmp_name']);
|
||||
if (!$width || !$height) {
|
||||
self::$error = 'Не подтянулись размеры файла.';
|
||||
return false;
|
||||
@@ -123,7 +133,7 @@ class Uploader
|
||||
if (!$this->maxFileSizeMb) {
|
||||
$this->setMaxFileSize(2);
|
||||
}
|
||||
if ($this->FILE['size'] > $this->maxFileSizeMb * (1024 * 1024) || $this->FILE['size'] <= 0) {
|
||||
if ($this->file['size'] > $this->maxFileSizeMb * (1024 * 1024) || $this->file['size'] <= 0) {
|
||||
self::$error = 'Неверный размер файла. Максимальный размер файла ' . $this->maxFileSizeMb . ' МБ';
|
||||
return false;
|
||||
}
|
||||
@@ -133,8 +143,8 @@ class Uploader
|
||||
private function hasNormalType(): bool
|
||||
{
|
||||
if (
|
||||
!preg_match('/\.(' . $this->extensions . ')$/i', $this->FILE['name'], $this->extMatches) ||
|
||||
!preg_match('/image/i', $this->FILE['type'])
|
||||
!preg_match('/\.(' . $this->extensions . ')$/i', $this->file['name'], $this->extMatches) ||
|
||||
!preg_match('/image/i', $this->file['type'])
|
||||
) {
|
||||
self::$error = 'Неверный тип файла. Допустимые типы : ' . $this->extensions;
|
||||
return false;
|
||||
@@ -156,17 +166,25 @@ class Uploader
|
||||
$this->extMatches[1] = strtolower($this->extMatches[1]);
|
||||
$fn = uniqid('f_', true) . '.' . $this->extMatches[1];
|
||||
$fn2 = uniqid('f_', true) . '.gif';
|
||||
if ($this->cnm) {
|
||||
$fn = $this->cnm;
|
||||
$fn2 = $this->cnm;
|
||||
if ($this->customName) {
|
||||
$fn = $this->customName . '.' . pathinfo($this->file['name'], PATHINFO_EXTENSION);
|
||||
$fn2 = $this->customName;
|
||||
}
|
||||
if (!move_uploaded_file($this->FILE['tmp_name'], $this->savePath . $fn)) {
|
||||
if (!move_uploaded_file($this->file['tmp_name'], $this->savePath . $fn)) {
|
||||
self::$error = 'Ошибка загрузки файла';
|
||||
return false;
|
||||
}
|
||||
var_dump([$fn2, $fn, $this->savePath . $fn]);
|
||||
return [$fn2, $fn, $this->savePath . $fn];
|
||||
}
|
||||
|
||||
private function uploadToDatabase(): int
|
||||
{
|
||||
$id = time();
|
||||
Db::sql('insert into images (mime_type, img, id) VALUES (?,?,?)', [$this->file['type'], file_get_contents($this->file['tmp_name']), $id]);
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function saveimg()
|
||||
{
|
||||
return $this->hasNormalFilePath() &&
|
||||
@@ -174,4 +192,11 @@ class Uploader
|
||||
$this->hasNormalFileSize() &&
|
||||
$this->hasNormalType() ? $this->upload() : false;
|
||||
}
|
||||
}
|
||||
|
||||
public function saveToDb()
|
||||
{
|
||||
return $this->hasNormalDimensions() &&
|
||||
$this->hasNormalFileSize() &&
|
||||
$this->hasNormalType() ? $this->uploadToDatabase() : false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user