-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadedFile.php
executable file
·195 lines (171 loc) · 5.59 KB
/
UploadedFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
namespace MaplePHP\Http;
use RuntimeException;
use MaplePHP\Http\Interfaces\UploadedFileInterface;
use MaplePHP\Http\Interfaces\StreamInterface;
class UploadedFile implements UploadedFileInterface
{
public const MOVE_CHUNK_SIZE = 1024;
public const ERROR_PHRASE = [
UPLOAD_ERR_OK => "There is no error, the file uploaded with success",
UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified " .
"in the HTML form",
UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded",
UPLOAD_ERR_NO_FILE => "No file was uploaded",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory. This is a server error, please try again later",
UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk",
UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload"
];
private $stream;
private $size;
private $name;
private $type;
private $tmp;
private $error;
private $moved;
//private $target;
/**
* Prepare for upload
* Expects: $_FILES['uploadKey']
* StreamInterface
* (string) FilePath/php stream
*/
public function __construct(StreamInterface|array|string $stream, mixed ...$vars)
{
if(count($vars) > 0 && is_string($stream)) {
array_unshift($vars, $stream);
$stream = array_combine(['name', 'type', 'tmp_name', 'error', 'size'], $vars);
}
if ($stream instanceof StreamInterface) {
$this->stream = $stream;
} elseif (isset($stream['tmp_name'])) {
$this->name = $stream['name'];
$this->type = $stream['type'];
$this->tmp = $stream['tmp_name'];
$this->error = $stream['error'];
$this->size = $stream['size'];
} elseif (is_string($stream)) {
$this->stream = $this->withStream($stream);
} else {
throw new RuntimeException("Could not validate arguments for the upload stream", 1);
}
}
/**
* Get current stream if it exsists
* @return StreamInterface
*/
public function getStream(): StreamInterface
{
if (is_null($this->stream)) {
throw new RuntimeException("The no stream exists. You need to construct a new stream", 1);
}
if (is_string($this->stream)) {
$this->stream = $this->withStream($this->stream);
}
return $this->stream;
}
/**
* Move file/output to target path/file. Will throw RuntimeException on error
* @param string $targetPath
* @return void
*/
public function moveTo($targetPath): void
{
if ($this->moved) {
throw new RuntimeException('File has already been moved');
}
if (!is_writable(dirname($targetPath))) {
throw new RuntimeException('Target directory is not writable');
}
if (!is_null($this->stream)) {
$this->streamFile($targetPath);
} elseif (!is_null($this->tmp)) {
$this->moveUploadedFile($targetPath);
}
if (!$this->moved) {
throw new RuntimeException('Failed to move file to target path');
}
}
/**
* Stream file/output to target path/file
* @param string $targetPath
* @return void
*/
public function streamFile(string $targetPath): void
{
$stream = $this->getStream();
$stream->seek(0);
$targetStream = new Stream($targetPath, 'w');
while (!$stream->eof()) {
$targetStream->write($stream->read($this::MOVE_CHUNK_SIZE));
}
// Add file to stream so it is changeable after upload
$this->stream = $targetStream;
$this->moved = true;
}
/**
* Will be useing the PHP function move_uploaded_file to upload a file to target path/file
* @param string $targetPath
* @return int|bool
*/
public function moveUploadedFile(string $targetPath)
{
if (!is_uploaded_file($this->tmp)) {
throw new RuntimeException("Could not upload the file \"{$this->name}\" becouse of a conflict.");
}
$this->moved = move_uploaded_file($this->tmp, $targetPath);
// Add file to stream as a String, that way it will be prepared and
// without loading resource unless you call/follow up with the @getStream method
if ($this->moved) {
$this->stream = $targetPath;
}
return $this->moved;
}
/**
* Get file size
* @return int|null
*/
public function getSize(): ?int
{
return (is_null($this->size)) ? (($this->stream instanceof StreamInterface) ? $this->stream->getSize() : null) : $this->size;
}
/**
* Get error
* @return int
*/
public function getError()
{
return (int)$this->error;
}
/**
* Get error phrarse
* @return string
*/
public function getErrorPhrase(): ?string
{
return ($this::ERROR_PHRASE[$this->error] ?? null);
}
/**
* Get client file name
* @return string
*/
public function getClientFilename(): ?string
{
return $this->name;
}
/**
* Get client media type
* @return string
*/
public function getClientMediaType(): ?string
{
return $this->type;
}
public function withStream(string $stream): StreamInterface
{
$inst = new Stream($stream, 'r+');
return $inst;
}
}