【问题标题】:Different maxsizes for different type files in YiiYii 中不同类型文件的不同 maxsizes
【发布时间】:2012-10-31 12:42:13
【问题描述】:

我用 yii 上传文件,我想验证这些文件。 但是对于不同类型的文件 - 不同的大小。 例如 - 如果用户想要上传 jpg 或 pdf 文件 - maxSize - 10 MB。 - 对于视频文件 - maxSize - 150 MB。

我该怎么做?

此变体不起作用,因为仅适用于第二条规则。

public function rules()
{
    return array(
        array('files', 'validateFiles', 'types' => 'jpg, gif, png, pdf, doc, docx', 'maxSize' => 10 * 1024 * 1024, 'on' => 'upload'),
        array('files', 'validateFiles', 'types' => 'avi, mpg, flv, mov, mpeg, mp4, 3gp, wmv', 'maxSize' => 150 * 1024 * 1024, 'on' => 'upload'),
}

public function validateFiles($attribute, $params)
{
    $validator = CValidator::createValidator('file', $this, $attribute, $params);
    $files = array();
    foreach(CUploadedFile::getInstances($this, $attribute) as $file) {
        $this->$attribute = $file;
        $files[] = $file;
        $validator->validate($this, $attribute);
    }
    $this->$attribute = $files;
}

【问题讨论】:

    标签: file-upload yii


    【解决方案1】:

    您可以允许最大值。要上传的文件大小,然后在收到文件时验证 - 在模型或控制器中验证。无论如何,文件大小只有在文件上传后才会在 Yii 中验证。

    【讨论】:

      【解决方案2】:

      你这样做的方式行不通。

      如果代码对第一个数组进行验证,它将对第二个数组失败,反之亦然。除非满足这两个条件,否则您的模型将永远不会验证。

      实现这一点的最佳方法是扩展 validateFiles 函数并检查那里的扩展名/文件大小。

      例如,首先将规则功能更改为两者的组合

      public function rules()
      {
          return array(
              array('files', 'validateFiles', 'types' => 'jpg, gif, png, pdf, doc, docx, avi, mpg, flv, mov, mpeg, mp4, 3gp, wmv', 'maxSize' => 150 * 1024 * 1024, 'on' => 'upload')
          )
      }
      

      这样,文件类型的检查已经完成,并且文件总大小的检查也完成了。

      之后,更改 validateFiles 函数以检查扩展名/文件大小

      【讨论】:

        【解决方案3】:

        我的解决方案 - 创建新的验证器 - 例如 PictureVideoValidator 并在此文件中选择哪种文件类型并进行验证。

        <?php
        class PictureVideoValidator extends CValidator
        {
            public $allowEmpty = false;
            public $maxSizePicture = 4194304; // 4 Mb
            public $maxSizeVideo = 157286400; // 150 Mb
        
            protected function validateAttribute($object, $attribute)
            {
                $file = $object->$attribute;
        
                if (!$file instanceof CUploadedFile) {
                    $file = CUploadedFile::getInstance($object, $attribute);
                    if ($file == null)
                        return $this->emptyAttribute($object, $attribute);
                }
        
        
                $type = CFileHelper::getMimeType($file->tempName);
                list($type, $subtype) = @explode('/', $type);
        
                $allowedTypes = array('image', 'video');
        
                if (!in_array($type, $allowedTypes)) {
                    $message = Yii::t('ext', 'File cannot be uploaded because it is not a valid image/video file');
                    $this->addError($object, $attribute, $message);
                    return;
                }
        
                $fileSize = filesize($file->tempName);
                if ($type == 'image') {
                    $maxSize = $this->maxSizePicture;
                } else if ($type == 'video') {
                    $maxSize = $this->maxSizeVideo;
                }
        
                if ($fileSize > $maxSize) {
                    $message = Yii::t('ext', 'The file "{fileName}" is too large. Its size cannot exceed {maxSize} bytes.');
                    $this->addError($object, $attribute, $message, array(
                        '{fileName}' => $file->getName(),
                        '{maxSize}' => $this->maxSizePicture
                    ));
                }
            }
        
            protected function emptyAttribute($object, $attribute)
            {
                if (!$this->allowEmpty) {
                    $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.');
                    $this->addError($object, $attribute, $message);
                }
            }
        
        }
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-21
          • 2013-02-03
          • 2011-09-14
          • 1970-01-01
          • 2018-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多