1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Util |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2010-2013 Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A file uploaded through a form. |
13
|
|
|
* |
14
|
|
|
* <code> |
15
|
|
|
* |
16
|
|
|
* // Create a file instance. |
17
|
|
|
* $upload = new Uploaded( |
18
|
|
|
* $_FILES['tmp_name'], $_FILES['name'], $_FILES['type'], $_FILES['size'], $_FILES['error'] |
19
|
|
|
* ); |
20
|
|
|
* |
21
|
|
|
* if ($upload instanceof Uploaded) { |
22
|
|
|
* $upload->move('path/to/your/images/dir', $upload->getClientOriginalName()); |
23
|
|
|
* } |
24
|
|
|
* |
25
|
|
|
* </code> |
26
|
|
|
* |
27
|
|
|
* @package Util |
28
|
|
|
* @author Gjero Krsteski <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Uploaded extends File |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Whether the test mode is activated. |
34
|
|
|
* Local files are used in test mode hence the code should not enforce HTTP uploads. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $test = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The original name of the uploaded file. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $name; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The mime type provided by the uploader. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $mime; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The file size provided by the uploader. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $size; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The UPLOAD_ERR_XXX constant provided by the uploader. |
63
|
|
|
* |
64
|
|
|
* @var integer |
65
|
|
|
*/ |
66
|
|
|
private $error; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Accepts the information of the uploaded file as provided by the PHP global $_FILES. |
70
|
|
|
* |
71
|
|
|
* <code> |
72
|
|
|
* // Create a file instance. |
73
|
|
|
* $file = new Uploaded( |
74
|
|
|
* $_FILES['tmp_name'], $_FILES['name'], $_FILES['type'], $_FILES['size'], $_FILES['error'] |
75
|
|
|
* ); |
76
|
|
|
* </code> |
77
|
|
|
* |
78
|
|
|
* @param string $path The full temporary path to the file |
79
|
|
|
* @param string $name The original file name |
80
|
|
|
* @param string|null $mime The type of the file as provided by PHP |
81
|
|
|
* @param string|null $size The file size |
82
|
|
|
* @param int|null $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants) |
83
|
|
|
* @param bool $test Whether the test mode is active |
84
|
|
|
* |
85
|
|
|
* @throws \RuntimeException If file_uploads is disabled |
86
|
|
|
*/ |
87
|
|
|
public function __construct($path, $name, $mime = null, $size = null, $error = null, $test = false) |
88
|
|
|
{ |
89
|
|
|
if (!ini_get('file_uploads')) { |
90
|
|
|
throw new \RuntimeException('Unable to create file because "file_uploads" is disabled in your php.ini'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->name = $this->getName($name); |
94
|
|
|
$this->mime = $mime ?: 'application/octet-stream'; |
95
|
|
|
$this->size = $size; |
96
|
|
|
$this->error = $error ?: UPLOAD_ERR_OK; |
97
|
|
|
$this->test = (bool)$test; |
98
|
|
|
|
99
|
|
|
parent::__construct($path, UPLOAD_ERR_OK === $this->error); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the original file name. |
104
|
|
|
* |
105
|
|
|
* It is extracted from the request from which the file has been uploaded. |
106
|
|
|
* Then is should not be considered as a safe value. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getClientOriginalName() |
111
|
|
|
{ |
112
|
|
|
return $this->name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the file mime type. |
117
|
|
|
* |
118
|
|
|
* It is extracted from the request from which the file has been uploaded. |
119
|
|
|
* Then is should not be considered as a safe value. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getClientMimeType() |
124
|
|
|
{ |
125
|
|
|
return $this->mime; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the file size. |
130
|
|
|
* |
131
|
|
|
* It is extracted from the request from which the file has been uploaded. |
132
|
|
|
* Then is should not be considered as a safe value. |
133
|
|
|
* |
134
|
|
|
* @return string|null |
135
|
|
|
*/ |
136
|
|
|
public function getClientSize() |
137
|
|
|
{ |
138
|
|
|
return $this->size; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the upload error. |
143
|
|
|
* |
144
|
|
|
* If the upload was successful, the constant UPLOAD_ERR_OK is returned. |
145
|
|
|
* Otherwise one of the other UPLOAD_ERR_XXX constants is returned. |
146
|
|
|
* |
147
|
|
|
* @return integer |
148
|
|
|
*/ |
149
|
|
|
public function getError() |
150
|
|
|
{ |
151
|
|
|
return (int)$this->error; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns whether the file was uploaded successfully. |
156
|
|
|
* |
157
|
|
|
* @return boolean True if no error occurred during uploading |
158
|
|
|
*/ |
159
|
|
|
public function isValid() |
160
|
|
|
{ |
161
|
|
|
return $this->error === UPLOAD_ERR_OK; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Moves the file to a new location. |
166
|
|
|
* |
167
|
|
|
* @param string $dir |
168
|
|
|
* @param null $name |
169
|
|
|
* |
170
|
|
|
* @return \Pimf\Util\File |
171
|
|
|
* @throws \RuntimeException If the file has not been uploaded via Http or can not move the file. |
172
|
|
|
*/ |
173
|
|
|
public function move($dir, $name = null) |
174
|
|
|
{ |
175
|
|
|
if ($this->isValid()) { |
176
|
|
|
|
177
|
|
|
if ($this->test) { |
178
|
|
|
return parent::move($dir, $name); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (is_uploaded_file($this->getPathname())) { |
182
|
|
|
|
183
|
|
|
$target = $this->getTargetFile($dir, $name); |
184
|
|
|
|
185
|
|
|
move_uploaded_file($this->getPathname(), $target); |
186
|
|
|
|
187
|
|
|
chmod($target, 0666 & ~umask()); |
188
|
|
|
|
189
|
|
|
return $target; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
throw new \RuntimeException("The file {$this->getPathname()} has not been uploaded via Http"); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the maximum size of an uploaded file in bytes as configured in php.ini |
198
|
|
|
* |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
|
|
public static function getMaxFilesize() |
202
|
|
|
{ |
203
|
|
|
$max = trim(ini_get('upload_max_filesize')); |
204
|
|
|
|
205
|
|
|
if ('' === $max) { |
206
|
|
|
return PHP_INT_MAX; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$unit = strtolower(substr($max, -1)); |
210
|
|
|
$max = (int)substr($max, 0, -1); |
211
|
|
|
|
212
|
|
|
if (in_array($unit, array('g', 'm', 'k'), true)) { |
213
|
|
|
$max *= 1024; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $max; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|