1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Site; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A file upload. |
7
|
|
|
*/ |
8
|
|
|
class Upload |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The temporary path. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $path; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The client's name for the file. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $remoteName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The upload's "error" code. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $status; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $status |
34
|
|
|
* @param string $remoteName |
35
|
|
|
* @param string $path |
36
|
|
|
*/ |
37
|
|
|
public function __construct(int $status, string $remoteName, string $path) |
38
|
|
|
{ |
39
|
|
|
$this->status = $status; |
40
|
|
|
$this->remoteName = $remoteName; |
41
|
|
|
$this->path = $path; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generates and returns an {@link HttpError} if the status indicates one took place. |
46
|
|
|
* |
47
|
|
|
* @return null|HttpError |
48
|
|
|
*/ |
49
|
|
|
public function getError() |
50
|
|
|
{ |
51
|
|
|
switch ($this->status) { |
52
|
|
|
case UPLOAD_ERR_INI_SIZE: |
53
|
|
|
case UPLOAD_ERR_FORM_SIZE: |
54
|
|
|
return new HttpError(413, 'The file is too large.'); |
55
|
|
|
case UPLOAD_ERR_PARTIAL: |
56
|
|
|
return new HttpError(400, 'The file was only partially uploaded.'); |
57
|
|
|
case UPLOAD_ERR_NO_FILE: |
58
|
|
|
return new HttpError(400, 'No file was sent.'); |
59
|
|
|
case UPLOAD_ERR_NO_TMP_DIR: |
60
|
|
|
return new HttpError(507, 'Nowhere to store the file.'); |
61
|
|
|
case UPLOAD_ERR_CANT_WRITE: |
62
|
|
|
return new HttpError(507, 'Unable to write the file to storage.'); |
63
|
|
|
case UPLOAD_ERR_EXTENSION: |
64
|
|
|
return new HttpError(500, 'The file was rejected by the server.'); |
65
|
|
|
default: |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
final public function getPath(): string |
74
|
|
|
{ |
75
|
|
|
return $this->path; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
final public function getRemoteName(): string |
82
|
|
|
{ |
83
|
|
|
return $this->remoteName; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
final public function getSize(): int |
90
|
|
|
{ |
91
|
|
|
return filesize($this->path); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
final public function getStatus(): int |
98
|
|
|
{ |
99
|
|
|
return $this->status; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
final public function getType(): string |
106
|
|
|
{ |
107
|
|
|
return mime_content_type($this->path); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $path |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function move(string $path) |
115
|
|
|
{ |
116
|
|
|
move_uploaded_file($this->path, $path); |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|