Yii中不同类型文件的不同maxsizes

时间:2012-10-31 12:42:14

标签: file-upload yii

我用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;
}

3 个答案:

答案 0 :(得分:0)

你这样做的方式是行不通的。

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

实现这一目标的最佳方法是扩展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函数以检查extensions / filesize

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我的解决方案 - 创建新的Validator - 例如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);
        }
    }

}

?>