PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.20.0
GiveWP – Donation Plugin and Fundraising Platform v2.20.0
4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / symfony / http-foundation / File / UploadedFile.php
give / vendor / symfony / http-foundation / File Last commit date
Exception 4 years ago MimeType 4 years ago File.php 4 years ago Stream.php 4 years ago UploadedFile.php 4 years ago
UploadedFile.php
282 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpFoundation\File;
13
14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16 use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
17
18 /**
19 * A file uploaded through a form.
20 *
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
23 * @author Fabien Potencier <fabien@symfony.com>
24 */
25 class UploadedFile extends File
26 {
27 private $test;
28 private $originalName;
29 private $mimeType;
30 private $size;
31 private $error;
32
33 /**
34 * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
35 *
36 * The file object is only created when the uploaded file is valid (i.e. when the
37 * isValid() method returns true). Otherwise the only methods that could be called
38 * on an UploadedFile instance are:
39 *
40 * * getClientOriginalName,
41 * * getClientMimeType,
42 * * isValid,
43 * * getError.
44 *
45 * Calling any other method on an non-valid instance will cause an unpredictable result.
46 *
47 * @param string $path The full temporary path to the file
48 * @param string $originalName The original file name of the uploaded file
49 * @param string|null $mimeType The type of the file as provided by PHP; null defaults to application/octet-stream
50 * @param int|null $size The file size provided by the uploader
51 * @param int|null $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
52 * @param bool $test Whether the test mode is active
53 * Local files are used in test mode hence the code should not enforce HTTP uploads
54 *
55 * @throws FileException If file_uploads is disabled
56 * @throws FileNotFoundException If the file does not exist
57 */
58 public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
59 {
60 $this->originalName = $this->getName($originalName);
61 $this->mimeType = $mimeType ?: 'application/octet-stream';
62 $this->size = $size;
63 $this->error = $error ?: \UPLOAD_ERR_OK;
64 $this->test = (bool) $test;
65
66 parent::__construct($path, \UPLOAD_ERR_OK === $this->error);
67 }
68
69 /**
70 * Returns the original file name.
71 *
72 * It is extracted from the request from which the file has been uploaded.
73 * Then it should not be considered as a safe value.
74 *
75 * @return string The original name
76 */
77 public function getClientOriginalName()
78 {
79 return $this->originalName;
80 }
81
82 /**
83 * Returns the original file extension.
84 *
85 * It is extracted from the original file name that was uploaded.
86 * Then it should not be considered as a safe value.
87 *
88 * @return string The extension
89 */
90 public function getClientOriginalExtension()
91 {
92 return pathinfo($this->originalName, \PATHINFO_EXTENSION);
93 }
94
95 /**
96 * Returns the file mime type.
97 *
98 * The client mime type is extracted from the request from which the file
99 * was uploaded, so it should not be considered as a safe value.
100 *
101 * For a trusted mime type, use getMimeType() instead (which guesses the mime
102 * type based on the file content).
103 *
104 * @return string The mime type
105 *
106 * @see getMimeType()
107 */
108 public function getClientMimeType()
109 {
110 return $this->mimeType;
111 }
112
113 /**
114 * Returns the extension based on the client mime type.
115 *
116 * If the mime type is unknown, returns null.
117 *
118 * This method uses the mime type as guessed by getClientMimeType()
119 * to guess the file extension. As such, the extension returned
120 * by this method cannot be trusted.
121 *
122 * For a trusted extension, use guessExtension() instead (which guesses
123 * the extension based on the guessed mime type for the file).
124 *
125 * @return string|null The guessed extension or null if it cannot be guessed
126 *
127 * @see guessExtension()
128 * @see getClientMimeType()
129 */
130 public function guessClientExtension()
131 {
132 $type = $this->getClientMimeType();
133 $guesser = ExtensionGuesser::getInstance();
134
135 return $guesser->guess($type);
136 }
137
138 /**
139 * Returns the file size.
140 *
141 * It is extracted from the request from which the file has been uploaded.
142 * Then it should not be considered as a safe value.
143 *
144 * @return int|null The file size
145 */
146 public function getClientSize()
147 {
148 return $this->size;
149 }
150
151 /**
152 * Returns the upload error.
153 *
154 * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
155 * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
156 *
157 * @return int The upload error
158 */
159 public function getError()
160 {
161 return $this->error;
162 }
163
164 /**
165 * Returns whether the file was uploaded successfully.
166 *
167 * @return bool True if the file has been uploaded with HTTP and no error occurred
168 */
169 public function isValid()
170 {
171 $isOk = \UPLOAD_ERR_OK === $this->error;
172
173 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
174 }
175
176 /**
177 * Moves the file to a new location.
178 *
179 * @param string $directory The destination folder
180 * @param string $name The new file name
181 *
182 * @return File A File object representing the new file
183 *
184 * @throws FileException if, for any reason, the file could not have been moved
185 */
186 public function move($directory, $name = null)
187 {
188 if ($this->isValid()) {
189 if ($this->test) {
190 return parent::move($directory, $name);
191 }
192
193 $target = $this->getTargetFile($directory, $name);
194
195 set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
196 $moved = move_uploaded_file($this->getPathname(), $target);
197 restore_error_handler();
198 if (!$moved) {
199 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
200 }
201
202 @chmod($target, 0666 & ~umask());
203
204 return $target;
205 }
206
207 throw new FileException($this->getErrorMessage());
208 }
209
210 /**
211 * Returns the maximum size of an uploaded file as configured in php.ini.
212 *
213 * @return int The maximum size of an uploaded file in bytes
214 */
215 public static function getMaxFilesize()
216 {
217 $sizePostMax = self::parseFilesize(ini_get('post_max_size'));
218 $sizeUploadMax = self::parseFilesize(ini_get('upload_max_filesize'));
219
220 return min($sizePostMax ?: \PHP_INT_MAX, $sizeUploadMax ?: \PHP_INT_MAX);
221 }
222
223 /**
224 * Returns the given size from an ini value in bytes.
225 *
226 * @return int The given size in bytes
227 */
228 private static function parseFilesize($size)
229 {
230 if ('' === $size) {
231 return 0;
232 }
233
234 $size = strtolower($size);
235
236 $max = ltrim($size, '+');
237 if (0 === strpos($max, '0x')) {
238 $max = \intval($max, 16);
239 } elseif (0 === strpos($max, '0')) {
240 $max = \intval($max, 8);
241 } else {
242 $max = (int) $max;
243 }
244
245 switch (substr($size, -1)) {
246 case 't': $max *= 1024;
247 // no break
248 case 'g': $max *= 1024;
249 // no break
250 case 'm': $max *= 1024;
251 // no break
252 case 'k': $max *= 1024;
253 }
254
255 return $max;
256 }
257
258 /**
259 * Returns an informative upload error message.
260 *
261 * @return string The error message regarding the specified error code
262 */
263 public function getErrorMessage()
264 {
265 static $errors = [
266 \UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
267 \UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
268 \UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
269 \UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
270 \UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
271 \UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
272 \UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
273 ];
274
275 $errorCode = $this->error;
276 $maxFilesize = \UPLOAD_ERR_INI_SIZE === $errorCode ? self::getMaxFilesize() / 1024 : 0;
277 $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
278
279 return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
280 }
281 }
282