1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* UploadedFile.php - This class represents an uploaded file. |
5
|
|
|
* |
6
|
|
|
* @package jaxon-core |
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
8
|
|
|
* @copyright 2017 Thierry Feuzeu <[email protected]> |
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Jaxon\Request\Support; |
14
|
|
|
|
15
|
|
|
class UploadedFile |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The uploaded file type |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $sType; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The uploaded file name, without the extension and slugified |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $sName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The uploaded file name, with the extension |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $sFilename; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The uploaded file path |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $sPath; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The uploaded file size |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $sSize; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The uploaded file extension |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $sExtension; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create an instance of this class using data from the $_FILES global var. |
61
|
|
|
* |
62
|
|
|
* @param string $sUploadDir The directory where to save the uploaded file |
63
|
|
|
* @param array $aFile The uploaded file data |
64
|
|
|
* |
65
|
|
|
* @return UploadedFile |
66
|
|
|
*/ |
67
|
|
|
public static function fromHttpData($sUploadDir, array $aFile) |
68
|
|
|
{ |
69
|
|
|
$xFile = new UploadedFile(); |
70
|
|
|
$xFile->sType = $aFile['type']; |
71
|
|
|
$xFile->sName = $xFile->slugify($aFile['filename']); |
72
|
|
|
$xFile->sFilename = $aFile['name']; |
73
|
|
|
$xFile->sExtension = $aFile['extension']; |
74
|
|
|
$xFile->sSize = $aFile['size']; |
75
|
|
|
$xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension; |
76
|
|
|
return $xFile; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Convert the UploadedFile instance to array. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function toTempData() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'type' => $this->sType, |
88
|
|
|
'name' => $this->sName, |
89
|
|
|
'filename' => $this->sFilename, |
90
|
|
|
'extension' => $this->sExtension, |
91
|
|
|
'size' => $this->sSize, |
92
|
|
|
'path' => $this->sPath, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create an instance of this class using data from an array. |
98
|
|
|
* |
99
|
|
|
* @param array $aFile The uploaded file data |
100
|
|
|
* |
101
|
|
|
* @return UploadedFile |
102
|
|
|
*/ |
103
|
|
|
public static function fromTempData(array $aFile) |
104
|
|
|
{ |
105
|
|
|
$xFile = new UploadedFile(); |
106
|
|
|
$xFile->sType = $aFile['type']; |
107
|
|
|
$xFile->sName = $aFile['name']; |
108
|
|
|
$xFile->sFilename = $aFile['filename']; |
109
|
|
|
$xFile->sExtension = $aFile['extension']; |
110
|
|
|
$xFile->sSize = $aFile['size']; |
111
|
|
|
$xFile->sPath = $aFile['path']; |
112
|
|
|
return $xFile; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Slugify a text |
117
|
|
|
* |
118
|
|
|
* @var string |
119
|
|
|
*/ |
120
|
|
|
protected function slugify($sText) |
121
|
|
|
{ |
122
|
|
|
// Todo: slugify the text. |
123
|
|
|
return $sText; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the uploaded file type |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function type() |
132
|
|
|
{ |
133
|
|
|
return $this->sType; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the uploaded file name, without the extension and slugified |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function name() |
142
|
|
|
{ |
143
|
|
|
return $this->sName; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the uploaded file name, with the extension |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function filename() |
152
|
|
|
{ |
153
|
|
|
return $this->sFilename; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the uploaded file path |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function path() |
162
|
|
|
{ |
163
|
|
|
return $this->sPath; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the uploaded file size |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function size() |
172
|
|
|
{ |
173
|
|
|
return $this->sSize; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the uploaded file extension |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function extension() |
182
|
|
|
{ |
183
|
|
|
return $this->sExtension; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|