/**
  * Internally validates a file object.
  * @param CModel $object the object being validated
  * @param string $attribute the attribute being validated
  * @param CUploadedFile $file uploaded file passed to check against a set of rules
  * @throws CException if failed to upload the file
  */
 protected function validateFile($object, $attribute, $file)
 {
     if (null === $file || ($error = $file->getError()) == UPLOAD_ERR_NO_FILE) {
         return $this->emptyAttribute($object, $attribute);
     } elseif ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE || $this->maxSize !== null && $file->getSize() > $this->maxSize) {
         $message = $this->tooLarge !== null ? $this->tooLarge : Yii::t('yii', 'The file "{file}" is too large. Its size cannot exceed {limit} bytes.');
         $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->getSizeLimit()));
     } elseif ($error == UPLOAD_ERR_PARTIAL) {
         throw new CException(Yii::t('yii', 'The file "{file}" was only partially uploaded.', array('{file}' => $file->getName())));
     } elseif ($error == UPLOAD_ERR_NO_TMP_DIR) {
         throw new CException(Yii::t('yii', 'Missing the temporary folder to store the uploaded file "{file}".', array('{file}' => $file->getName())));
     } elseif ($error == UPLOAD_ERR_CANT_WRITE) {
         throw new CException(Yii::t('yii', 'Failed to write the uploaded file "{file}" to disk.', array('{file}' => $file->getName())));
     } elseif (defined('UPLOAD_ERR_EXTENSION') && $error == UPLOAD_ERR_EXTENSION) {
         // available for PHP 5.2.0 or above
         throw new CException(Yii::t('yii', 'A PHP extension stopped the file upload.'));
     }
     if ($this->minSize !== null && $file->getSize() < $this->minSize) {
         $message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.');
         $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->minSize));
     }
     if ($this->types !== null) {
         if (is_string($this->types)) {
             $types = preg_split('/[\\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY);
         } else {
             $types = $this->types;
         }
         if (!in_array(strtolower($file->getExtensionName()), $types)) {
             $message = $this->wrongType !== null ? $this->wrongType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{extensions}' => implode(', ', $types)));
         }
     }
     if ($this->mimeTypes !== null) {
         if (function_exists('finfo_open')) {
             $mimeType = false;
             if ($info = finfo_open(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
                 $mimeType = finfo_file($info, $file->getTempName());
             }
         } elseif (function_exists('mime_content_type')) {
             $mimeType = mime_content_type($file->getTempName());
         } else {
             throw new CException(Yii::t('yii', 'In order to use MIME-type validation provided by CFileValidator fileinfo PECL extension should be installed.'));
         }
         if (is_string($this->mimeTypes)) {
             $mimeTypes = preg_split('/[\\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
         } else {
             $mimeTypes = $this->mimeTypes;
         }
         if ($mimeType === false || !in_array(strtolower($mimeType), $mimeTypes)) {
             $message = $this->wrongMimeType !== null ? $this->wrongMimeType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files of these MIME-types are allowed: {mimeTypes}.');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{mimeTypes}' => implode(', ', $mimeTypes)));
         }
     }
 }
 /**
  * @param CUploadedFile $image
  * @return bool
  */
 public static function hasErrors(CUploadedFile $image)
 {
     return !(!$image->getError() && self::isAllowedExt($image) === true && self::isAllowedSize($image) === true && self::isAllowedType($image) === true);
 }
 /**
  * Internally validates a file object.
  *
  * @param CModel $object the object being validated
  * @param string $attribute the attribute being validated
  * @param CUploadedFile $file uploaded file passed to check against a set of rules
  */
 protected function validateFile($object, $attribute, $file)
 {
     if (null === $file || ($error = $file->getError()) == UPLOAD_ERR_NO_FILE) {
         return $this->emptyAttribute($object, $attribute);
     } else {
         if ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE || $this->maxSize !== null && $file->getSize() > $this->maxSize) {
             $message = $this->tooLarge !== null ? $this->tooLarge : Yii::t('yii', 'The file "{file}" is too large. Its size cannot exceed {limit} bytes.');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->getSizeLimit()));
         } else {
             if ($error == UPLOAD_ERR_PARTIAL) {
                 throw new CException(Yii::t('yii', 'The file "{file}" was only partially uploaded.', array('{file}' => $file->getName())));
             } else {
                 if ($error == UPLOAD_ERR_NO_TMP_DIR) {
                     throw new CException(Yii::t('yii', 'Missing the temporary folder to store the uploaded file "{file}".', array('{file}' => $file->getName())));
                 } else {
                     if ($error == UPLOAD_ERR_CANT_WRITE) {
                         throw new CException(Yii::t('yii', 'Failed to write the uploaded file "{file}" to disk.', array('{file}' => $file->getName())));
                     } else {
                         if (defined('UPLOAD_ERR_EXTENSION') && $error == UPLOAD_ERR_EXTENSION) {
                             throw new CException(Yii::t('yii', 'File upload was stopped by extension.'));
                         }
                     }
                 }
             }
         }
     }
     if ($this->minSize !== null && $file->getSize() < $this->minSize) {
         $message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.');
         $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{limit}' => $this->minSize));
     }
     if ($this->types !== null) {
         if (is_string($this->types)) {
             $types = preg_split('/[\\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY);
         } else {
             $types = $this->types;
         }
         if (!in_array(strtolower($file->getExtensionName()), $types)) {
             $message = $this->wrongType !== null ? $this->wrongType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{extensions}' => implode(', ', $types)));
         }
     }
 }
Beispiel #4
0
 private function _uploadImage(CUploadedFile $file)
 {
     $result = array('name' => $file->getName(), 'size' => $file->getSize(), 'tmpName' => $file->getTempName());
     if ($file->hasError) {
         $result['error'] = $file->getError();
     }
     return $result;
 }