<?php
namespace wcf\system\upload;
+use wcf\util\StringUtil;
/**
* Represents a file upload.
return $this->filename;
}
+ /**
+ * Returns the extension of the original file name.
+ *
+ * @return string
+ */
+ public function getFileExtension() {
+ if (($position = StringUtil::lastIndexOf($this->getFilename(), '.')) !== false) {
+ return StringUtil::substring($this->getFilename(), $position + 1);
+ }
+
+ return '';
+ }
+
/**
* Returns the file location.
*
* @return boolean
*/
public function validateFile($maxFilesize, array $fileExtensions) {
+ if ($this->errorCode != 0) {
+ $this->validationErrorType = 'uploadFailed';
+ return false;
+ }
+
+ if ($this->getFilesize() > $maxFilesize) {
+ $this->validationErrorType = 'tooLarge';
+ return false;
+ }
+ if (!in_array($this->getFileExtension(), $fileExtensions)) {
+ $this->validationErrorType = 'invalidExtension';
+ return false;
+ }
}
/**
* Returns the validation error type.
*
- * @return string
+ * @return string
*/
public function getValidationErrorType() {
return $this->validationErrorType;
}
+ /**
+ * Gets image data.
+ *
+ * @return array
+ */
public function getImageData() {
if (strpos($this->getMimeType(), 'image/') == 0) {
if (($imageData = @getImageSize($this->getLocation())) !== false) {
* list of validation errors.
* @var array
*/
- protected $errors = array();
+ protected $erroneousFiles = array();
/**
* Creates a new UploadHandler object.
$result = true;
foreach ($this->files as $file) {
if (!$file->validateFile($maxFilesize, $fileExtensions)) {
- $this->errors[$file->getFilename()] = $file->getValidationErrorType();
+ $this->erroneousFiles[] = $file;
$result = false;
}
}
}
/**
- * Returns a list of validation errors.
+ * Returns a list of erroneous files.
*
- * @return array
+ * @return array<wcf\system\upload\UploadFile>
*/
- public function getErrors() {
- return $this->errors;
+ public function getErroneousFiles() {
+ return $this->erroneousFiles;
}
/**
*/
public function saveFiles(IUploadFileSaveStrategy $saveStrategy) {
foreach ($this->files as $file) {
- $saveStrategy->save($file);
+ if (!$file->getValidationErrorType()) {
+ $saveStrategy->save($file);
+ }
}
}