Skip to content

Commit 8fbea47

Browse files
committed
Create UploadedFile.php
1 parent c15b522 commit 8fbea47

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

src/UploadedFile.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Класс загруженного файла.
4+
* @package evas-php\evas-http
5+
* @author Egor Vasyakin <[email protected]>
6+
*/
7+
namespace Evas\Http;
8+
9+
use Evas\Http\Interfaces\UploadedFileInterface;
10+
11+
class UploadedFile implements UploadedFileInterface
12+
{
13+
/** @var string|null клиентское имя файла */
14+
public $name;
15+
16+
/** @var string|null клиентское расширение имени файла */
17+
public $extension;
18+
19+
/** @var string|null клиентский тип файла */
20+
public $mimeType;
21+
22+
/** @var string временный путь файла */
23+
public $tmpPath;
24+
25+
/** @var string|null ошибка загрузки файла */
26+
public $error;
27+
28+
/** @var int размер файла */
29+
public $size;
30+
31+
/** @var string путь файла после перемещения */
32+
public $movedPath;
33+
34+
/**
35+
* Конструктор.
36+
* @param array маппинг свойств файла
37+
*/
38+
public function __construct(array $props)
39+
{
40+
$this->name = $props['name'] ?? null;
41+
$this->mimeType = $props['type'] ?? null;
42+
$this->tmpPath = $props['tmp_name'] ?? null;
43+
$this->error = $props['error'] ?? null;
44+
$this->size = $props['size'] ?? null;
45+
if (!empty($this->name)) {
46+
$this->extension = strtolower(@pathinfo($this->name, PATHINFO_EXTENSION));
47+
}
48+
}
49+
50+
/**
51+
* Перемещение загруженного файла.
52+
* @param string новый путь
53+
*/
54+
public function move(string $to)
55+
{
56+
$dir = dirname($to);
57+
if (!is_dir($dir)) mkdir($dir, 0777, true);
58+
59+
$extension = substr($to, strrpos($to, '.') + 1);
60+
if ($extension != $this->extension) {
61+
$to .= ".$this->extension";
62+
}
63+
$this->movedPath = $to;
64+
return move_uploaded_file($this->tmpPath, $to);
65+
}
66+
67+
/**
68+
* Получение размера файла.
69+
* @return int
70+
*/
71+
public function getSize(): int
72+
{
73+
return $this->size;
74+
}
75+
76+
/**
77+
* Получение ошибки файла.
78+
* @return string|null
79+
*/
80+
public function getError(): ?string
81+
{
82+
return $this->error;
83+
}
84+
85+
/**
86+
* Получение клиентского имени файла.
87+
* @return string|null
88+
*/
89+
public function getName(): ?string
90+
{
91+
return $this->name;
92+
}
93+
94+
/**
95+
* Получение расширения клиентского имени файла.
96+
* @return string|null
97+
*/
98+
public function getExtension(): ?string
99+
{
100+
return $this->extension;
101+
}
102+
103+
/**
104+
* Получение клиентского типа файла.
105+
* @return string|null
106+
*/
107+
public function getMediaType(): ?string
108+
{
109+
return $this->mimeType;
110+
}
111+
112+
/**
113+
* Получение пути файла после перемещения.
114+
* @return string|null
115+
*/
116+
public function getMovedPath(): ?string
117+
{
118+
return $this->movedPath;
119+
}
120+
}

0 commit comments

Comments
 (0)