2012-10-31 65 views
1

我與上傳文件警予,我想覈實這些文件。 但對於不同類型的文件 - 不同的大小。例如 - 如果用戶想要上傳jpg或pdf文件 - maxSize - 10 MB。 - 用於視頻文件 - maxSize - 150 MB。不同maxsizes針對不同類型的文件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

我的解決方案 - 創建新的驗證器 - 例如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功能,並檢查擴展/的filesizes存在的最好方法。

例如,首先改變規則功能的兩個

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纔會對文件大小進行驗證。

相關問題