/** * {@inheritdoc} */ public function validate(FileInfo $file) { if (!in_array($file->getMimeType(), $this->allowTypes)) { $this->errorCode = ErrorStore::ERROR_CUSTOM_MIME_TYPE; $this->errorMsg = 'File type is not valid'; return false; } return true; }
/** * {@inheritdoc} */ public function validate(FileInfo $file) { if (($code = $file->getError()) != 0) { $this->errorCode = $code; $this->errorMsg = $this->messages[$code]; return false; } return true; }
/** * {@inheritdoc} */ public function validate(FileInfo $file) { $size = $file->getSize(); $res = $size <= $this->maxSize; if (!is_null($this->minSize)) { $res = $size >= $this->minSize; } if (!$res) { $this->errorCode = ErrorStore::ERROR_CUSTOM_SIZE; $this->errorMsg = 'File size is not valid'; } return $res; }
/** * 移动文件 * 非合法上传文件和因其它未知原因造成的无法移动会抛出异常 * @param FileInfo $file * @return boolean * @throws UploadException */ protected function moveUploadFile(FileInfo $file) { $tmpName = $file->getTmpName(); $dest = $this->generateFilename($file); if (is_uploaded_file($tmpName)) { if (!file_exists($dest) || $this->override) { if (!@move_uploaded_file($tmpName, $dest)) { throw new UploadException('Failed to move file'); } $file->setPath($dest); $file->hasError = false; return true; } else { $file->setErrorCode(ErrorStore::ERROR_SAME_NAME_FILE); $file->setErrorMsg(sprintf('File "%s" already exists', $file->getOriginName())); return false; } } throw new UploadException('Upload file is not valid'); }