2022-08-25 17:07:39 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2023-07-19 12:36:13 +00:00
|
|
|
|
use Core\Db;
|
|
|
|
|
|
2022-08-25 17:07:39 +00:00
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Единая функция для заливки файлов на сервер.
|
2022-08-25 17:07:39 +00:00
|
|
|
|
*
|
|
|
|
|
* @author Ivor Barhansky <me@lopar.space>
|
2022-12-19 20:16:24 +00:00
|
|
|
|
* @version 1
|
2022-08-25 17:07:39 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class Uploader
|
|
|
|
|
{
|
2023-04-01 11:47:32 +00:00
|
|
|
|
private array $width = ['min' => 0, 'max' => 0];
|
|
|
|
|
private array $height = ['min' => 0, 'max' => 0];
|
|
|
|
|
private int $maxFileSizeMb;
|
|
|
|
|
private string $savePath;
|
|
|
|
|
private string $extensions = 'jpg|png|jpeg|gif';
|
|
|
|
|
private array $extMatches = [];
|
2023-07-19 12:36:13 +00:00
|
|
|
|
private array $file;
|
|
|
|
|
private string $customName;
|
|
|
|
|
|
2023-04-01 11:47:32 +00:00
|
|
|
|
public static string $error;
|
2022-08-25 17:07:39 +00:00
|
|
|
|
|
2023-07-19 12:36:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param string $customName
|
|
|
|
|
*/
|
|
|
|
|
public function setCustomName(string $customName): void
|
|
|
|
|
{
|
|
|
|
|
$this->customName = $customName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __construct($name)
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
2022-12-19 20:16:24 +00:00
|
|
|
|
if (!isset($_FILES[$name])) {
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-19 12:36:13 +00:00
|
|
|
|
$this->file = $_FILES[$name];
|
2022-08-25 17:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-19 12:36:13 +00:00
|
|
|
|
* @param int $max
|
2023-04-01 11:47:32 +00:00
|
|
|
|
* @param int|null $min
|
2022-08-25 17:07:39 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-04-01 11:47:32 +00:00
|
|
|
|
public function setWidth(int $max, ?int $min = null)
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
$this->width['min'] = is_null($min) ? $max : $min;
|
|
|
|
|
$this->width['max'] = $max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-19 12:36:13 +00:00
|
|
|
|
* @param int $max
|
2023-04-01 11:47:32 +00:00
|
|
|
|
* @param int|null $min
|
2022-08-25 17:07:39 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-04-01 11:47:32 +00:00
|
|
|
|
public function setHeight(int $max, ?int $min = null)
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
$this->height['min'] = is_null($min) ? $max : $min;
|
|
|
|
|
$this->height['max'] = $max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $megabytes
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setMaxFileSize($megabytes)
|
|
|
|
|
{
|
2022-12-19 20:16:24 +00:00
|
|
|
|
$this->maxFileSizeMb = $megabytes;
|
2022-08-25 17:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* @param string $path путь от корня до конечной папки без открывающего слеша.
|
2022-08-25 17:07:39 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-04-01 11:47:32 +00:00
|
|
|
|
public function setSavePath(string $path)
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
2022-12-19 20:16:24 +00:00
|
|
|
|
$this->savePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $path;
|
2022-08-25 17:07:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|array $ext
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setExtentions($ext)
|
|
|
|
|
{
|
|
|
|
|
if (is_array($ext)) {
|
|
|
|
|
$arr = $ext;
|
|
|
|
|
} else {
|
|
|
|
|
$arr[] = $ext;
|
|
|
|
|
}
|
|
|
|
|
if (!$arr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->extensions = implode('|', $arr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $width
|
|
|
|
|
* @param int $height
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-04-01 11:47:32 +00:00
|
|
|
|
public function setDimensions(int $width, int $height)
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
$this->setWidth($width);
|
|
|
|
|
$this->setHeight($height);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 11:47:32 +00:00
|
|
|
|
private function hasNormalDimensions(): bool
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
2023-07-19 12:36:13 +00:00
|
|
|
|
[$width, $height] = getimagesize($this->file['tmp_name']);
|
2022-12-19 20:16:24 +00:00
|
|
|
|
if (!$width || !$height) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error = 'Не подтянулись размеры файла.';
|
2022-12-19 20:16:24 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-08-25 17:07:39 +00:00
|
|
|
|
if (
|
2022-12-19 20:16:24 +00:00
|
|
|
|
($width < $this->width['min'] || $width > $this->width['max']) ||
|
|
|
|
|
($height < $this->height['min'] || $height > $this->height['max'])
|
2022-08-25 17:07:39 +00:00
|
|
|
|
) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error = 'Требования к размеру: ';
|
2022-08-25 17:07:39 +00:00
|
|
|
|
if ($this->width['min'] !== $this->width['max'] || $this->height['min'] !== $this->height['max']) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error .= "от [{$this->width['min']} x {$this->height['min']}] до ";
|
2022-08-25 17:07:39 +00:00
|
|
|
|
}
|
2022-12-19 20:16:24 +00:00
|
|
|
|
self::$error .= "[{$this->width['max']} x {$this->height['max']}].";
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error .= " Текущий размер [$width x $height]";
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 11:47:32 +00:00
|
|
|
|
private function hasNormalFileSize(): bool
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (!$this->maxFileSizeMb) {
|
|
|
|
|
$this->setMaxFileSize(2);
|
|
|
|
|
}
|
2023-07-19 12:36:13 +00:00
|
|
|
|
if ($this->file['size'] > $this->maxFileSizeMb * (1024 * 1024) || $this->file['size'] <= 0) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error = 'Неверный размер файла. Максимальный размер файла ' . $this->maxFileSizeMb . ' МБ';
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 11:47:32 +00:00
|
|
|
|
private function hasNormalType(): bool
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (
|
2023-07-19 12:36:13 +00:00
|
|
|
|
!preg_match('/\.(' . $this->extensions . ')$/i', $this->file['name'], $this->extMatches) ||
|
|
|
|
|
!preg_match('/image/i', $this->file['type'])
|
2022-08-25 17:07:39 +00:00
|
|
|
|
) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error = 'Неверный тип файла. Допустимые типы : ' . $this->extensions;
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 11:47:32 +00:00
|
|
|
|
private function hasNormalFilePath(): bool
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (!$this->savePath || !is_dir($this->savePath)) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error = 'Ошибка загрузки: нет такой папки.';
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function upload()
|
|
|
|
|
{
|
|
|
|
|
$this->extMatches[1] = strtolower($this->extMatches[1]);
|
|
|
|
|
$fn = uniqid('f_', true) . '.' . $this->extMatches[1];
|
|
|
|
|
$fn2 = uniqid('f_', true) . '.gif';
|
2023-07-19 12:36:13 +00:00
|
|
|
|
if ($this->customName) {
|
|
|
|
|
$fn = $this->customName . '.' . pathinfo($this->file['name'], PATHINFO_EXTENSION);
|
|
|
|
|
$fn2 = $this->customName;
|
2022-08-25 17:07:39 +00:00
|
|
|
|
}
|
2023-07-19 12:36:13 +00:00
|
|
|
|
if (!move_uploaded_file($this->file['tmp_name'], $this->savePath . $fn)) {
|
2023-01-10 16:29:32 +00:00
|
|
|
|
self::$error = 'Ошибка загрузки файла';
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-07-19 12:36:13 +00:00
|
|
|
|
var_dump([$fn2, $fn, $this->savePath . $fn]);
|
2022-08-25 17:07:39 +00:00
|
|
|
|
return [$fn2, $fn, $this->savePath . $fn];
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-19 12:36:13 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 11:47:32 +00:00
|
|
|
|
public function saveimg()
|
2022-08-25 17:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
return $this->hasNormalFilePath() &&
|
|
|
|
|
$this->hasNormalDimensions() &&
|
|
|
|
|
$this->hasNormalFileSize() &&
|
|
|
|
|
$this->hasNormalType() ? $this->upload() : false;
|
|
|
|
|
}
|
2023-07-19 12:36:13 +00:00
|
|
|
|
|
|
|
|
|
public function saveToDb()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasNormalDimensions() &&
|
|
|
|
|
$this->hasNormalFileSize() &&
|
|
|
|
|
$this->hasNormalType() ? $this->uploadToDatabase() : false;
|
|
|
|
|
}
|
|
|
|
|
}
|