3 namespace wcf\system\upload
;
6 * Represents a file upload.
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System\Upload
19 protected $filename = '';
25 protected $internalFileID = 0;
28 * location of the uploaded file
31 protected $location = '';
37 protected $filesize = 0;
40 * file upload error code
43 protected $errorCode = 0;
49 protected $mimeType = '';
52 * validation error type
55 protected $validationErrorType = '';
58 * additional data for validation errors
61 protected $validationErrorAdditionalData = [];
64 * Creates a new UploadFile object.
66 * @param string $filename
67 * @param string $location
68 * @param int $filesize
69 * @param int $errorCode
70 * @param string $mimeType
72 * @throws \Exception if an error occurred during upload and debug mode is enabled
74 public function __construct($filename, $location, $filesize, $errorCode = 0, $mimeType = '')
76 if (\
preg_match('~^__wcf_([0-9]+)_(.*)~', $filename, $matches)) {
77 $this->internalFileID
= $matches[1];
78 $filename = $matches[2];
81 $this->filename
= $filename;
82 $this->location
= $location;
83 $this->filesize
= $filesize;
84 $this->errorCode
= $errorCode;
85 $this->mimeType
= $mimeType;
87 if (ENABLE_DEBUG_MODE
) {
89 case \UPLOAD_ERR_INI_SIZE
:
90 throw new \
Exception("The uploaded file is larger than PHP's `upload_max_filesize`.");
91 case \UPLOAD_ERR_FORM_SIZE
:
92 throw new \
Exception("The uploaded file is larger than the form's `MAX_FILE_SIZE`.");
93 case \UPLOAD_ERR_PARTIAL
:
94 throw new \
Exception("The uploaded file was only partially uploaded.");
95 case \UPLOAD_ERR_NO_FILE
:
96 throw new \
Exception("No file was uploaded.");
97 case \UPLOAD_ERR_NO_TMP_DIR
:
98 throw new \
Exception("There is no temporary folder where PHP can save the file.");
99 case \UPLOAD_ERR_CANT_WRITE
:
100 throw new \
Exception("The uploaded file could not be written to disk.");
101 case \UPLOAD_ERR_EXTENSION
:
102 throw new \
Exception("A PHP extension stopped the file upload.");
108 * Returns the original file name.
112 public function getFilename()
114 return $this->filename
;
118 * Returns internal file id.
122 public function getInternalFileID()
124 return $this->internalFileID
;
128 * Returns the extension of the original file name.
132 public function getFileExtension()
134 if (($position = \
mb_strrpos($this->getFilename(), '.')) !== false) {
135 return \
mb_strtolower(\
mb_substr($this->getFilename(), $position +
1));
142 * Returns the file location.
146 public function getLocation()
148 return $this->location
;
152 * Returns the file size.
156 public function getFilesize()
158 return $this->filesize
;
162 * Returns the MIME type.
166 public function getMimeType()
168 return $this->mimeType
;
172 * Returns the error code.
176 public function getErrorCode()
178 return $this->errorCode
;
182 * Sets the validation error type.
184 * @param string $validationErrorType
185 * @param array $additionalData
187 public function setValidationErrorType($validationErrorType, array $additionalData = [])
189 $this->validationErrorType
= $validationErrorType;
190 $this->validationErrorAdditionalData
= $additionalData;
194 * Returns the validation error type.
198 public function getValidationErrorType()
200 return $this->validationErrorType
;
204 * Returns the validation error additional data array.
208 public function getValidationErrorAdditionalData()
210 return $this->validationErrorAdditionalData
;
214 * Returns the image data of the file or `null` if the file is no image.
218 public function getImageData()
220 if (($imageData = @\
getimagesize($this->getLocation())) !== false) {
222 'width' => $imageData[0],
223 'height' => $imageData[1],
224 'mimeType' => $imageData['mime'],
230 * Moves the uploaded file to the given location and updates the internal location value to the new location
231 * and the internal filename value to the new filename derived from the given location.
233 * @param string $newLocation new file location
235 public function moveUploadedFile($newLocation)
237 \
move_uploaded_file($this->location
, $newLocation);
239 $this->location
= $newLocation;
240 $this->filename
= \basename
($this->location
);