|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the miBadger package. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Michael Webbers <[email protected]> |
|
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
|
8
|
|
|
* @version 1.0.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace miBadger\Http; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The uploaded file class |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://www.php-fig.org/psr/psr-7/ |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class UploadedFile implements UploadedFileInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var string The name */ |
|
24
|
|
|
private $name; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string The type. */ |
|
27
|
|
|
private $type; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string The tmp name. */ |
|
30
|
|
|
private $tmpName; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int The error. */ |
|
33
|
|
|
private $error; |
|
34
|
|
|
|
|
35
|
|
|
/** @var int The size. */ |
|
36
|
|
|
private $size; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Construct a Stream object with the given name, type, tmp name, error and size. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @param string $type |
|
43
|
|
|
* @param string $tmpName |
|
44
|
|
|
* @param int $error |
|
45
|
|
|
* @param int $size |
|
46
|
|
|
*/ |
|
47
|
24 |
|
public function __construct($name, $type, $tmpName, $error, $size) |
|
48
|
|
|
{ |
|
49
|
24 |
|
$this->name = $name; |
|
50
|
24 |
|
$this->type = $type; |
|
51
|
24 |
|
$this->tmpName = $tmpName; |
|
52
|
24 |
|
$this->error = $error; |
|
53
|
24 |
|
$this->size = $size; |
|
54
|
24 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function getStream() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return new Stream(fopen($this->tmpName, 'r')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function moveTo($targetPath) |
|
68
|
|
|
{ |
|
69
|
3 |
|
if ($this->getError() != UPLOAD_ERR_OK || !is_uploaded_file($this->tmpName) || !move_uploaded_file($this->tmpName, $targetPath)) { |
|
70
|
2 |
|
throw new \RuntimeException('Can\'t move the file'); |
|
71
|
|
|
} |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getSize() |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->size; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function getError() |
|
86
|
|
|
{ |
|
87
|
4 |
|
return $this->error; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getClientFilename() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->name; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getClientMediaType() |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->type; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|