name = $file['name']; $this->type = $file['type']; $this->tmp_name = $file['tmp_name']; $this->error = $file['error']; $this->size = $file['size']; } /** * Get content of uploaded file. * * @throws Exception * @return string */ public function getContent() { if (!$this->isValid()) { throw new Exception($this->getErrorMessage()); } return file_get_contents($this->tmp_name); } /** * Get extension of uploaded file. * * @throws Exception * @return string */ public function getExtension() { if (!$this->isValid()) { throw new Exception($this->getErrorMessage()); } return pathinfo($this->name, PATHINFO_EXTENSION); } /** * Get size in bytes of uploaded file. * * @return int */ public function getSize() { return $this->size; } /** * Get error. * * @return int */ public function getError() { return $this->error; } /** * Returns true if file was uploaded without errors. * * @return bool */ public function isValid() { return ($this->error == UPLOAD_ERR_OK); } /** * Validate image size. * * @throws Exception if image size is 1MB or greater. */ public function validateImageSize() { if ($this->error === UPLOAD_ERR_INI_SIZE || $this->size > ZBX_MAX_IMAGE_SIZE) { throw new Exception(_s('Image size must be less than %1$s.', convertUnits([ 'value' => ZBX_MAX_IMAGE_SIZE, 'units' => 'B' ]))); } } /** * Return true if file was uploaded, maybe with errors. * * @return bool */ public function wasUploaded() { return ($this->error != UPLOAD_ERR_NO_FILE); } /** * Get error message. * * @return string */ protected function getErrorMessage() { switch ($this->error) { case UPLOAD_ERR_OK: return ''; case UPLOAD_ERR_INI_SIZE: return _s('File is too big, max upload size is %1$s bytes.', self::getMaxUploadSize()); case UPLOAD_ERR_NO_FILE: return _('No file was uploaded.'); default: return _('Incorrect file upload.'); } } }