2012-10-31 2 views
1

yii로 파일을 업로드하고 해당 파일을 확인하고 싶습니다. 그러나 다른 유형의 파일 - 크기가 다릅니다. 예 : - 업로드하려는 경우 jpg 또는 pdf 파일 - maxSize - 10MB. - 동영상 파일 용 - maxSize - 150MB.Yii의 다른 유형 파일에 대한 최대 크기가 다릅니다.

어떻게 할 수 있습니까?

이 변형은 작동하지 않는 두 번째 규칙이므로 작동하지 않습니다.

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

답변

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); 
     } 
    } 

} 

?> 
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 당신은 최대 허용 할 수

0

확장/파일 크기를 확인하는 기능을 변경합니다. 파일 크기를 업로드 한 다음 파일을 확인할 때 유효성 검사 - 모델 또는 컨트롤러에서 유효성을 검사합니다. 어쨌든, 파일 크기는 파일이 업로드 된 후에 Yii에서만 유효합니다.

관련 문제