File upload Yii中不同类型文件的不同最大大小

File upload Yii中不同类型文件的不同最大大小,file-upload,yii,File Upload,Yii,我用yii上传文件,我想验证这些文件。 但是对于不同类型的文件-不同的大小。 例如 -如果用户想要上传jpg或pdf文件-最大大小-10 MB。 -对于视频文件-最大大小-150 MB 我怎么能这样做呢 此变体不起作用,因为只适用于第二条规则 public function rules() { return array( array('files', 'validateFiles', 'types' => 'jpg, gif, png, pdf, doc, docx

我用yii上传文件,我想验证这些文件。 但是对于不同类型的文件-不同的大小。 例如 -如果用户想要上传jpg或pdf文件-最大大小-10 MB。 -对于视频文件-最大大小-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;
}

你这样做是行不通的

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

实现这一点的最佳方法是扩展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函数以检查扩展名/文件大小,这样做将不起作用

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

实现这一点的最佳方法是扩展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函数以检查扩展名/文件大小

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

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

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

}

?>

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

}

?>