Yii Framework v1.1.10 绫诲弬鑰

CFileValidator

system.validators
缁ф壙 class CFileValidator » CValidator » CComponent
婧愯嚜 1.0
鐗堟湰 $Id: CFileValidator.php 3491 2011-12-17 05:17:57Z jefftulsa $
婧愮爜 framework/validators/CFileValidator.php
CFileValidator楠岃瘉涓涓睘鎬ф槸鍚︽帴鏀跺埌涓涓湁鏁堢殑涓婁紶鏂囦欢銆

瀹冧娇鐢ㄦā鍨嬬被鐨勫睘鎬у悕妫绱笂浼犳枃浠剁殑淇℃伅锛岀劧鍚庢楠岃繖涓 鏂囦欢鏄惁宸茶鎴愬姛涓婁紶锛屼笂浼犳枃浠剁殑澶у皬鏄惁鏈秴杩囬檺鍒讹紝 涓婁紶鏂囦欢鐨勭被鍨嬫槸鍚﹁鍏佽銆

濡傛灉姝ゅ睘鎬т箣鍓嶆湭琚祴鍊硷紝姝ら獙璇佸櫒灏嗗皾璇曟彁鍙栦笂浼犵殑鏁版嵁銆 璇锋敞鎰忥細濡傛灉浣跨敤浜嗚〃鏍煎紡杈撳叆锛屾鍔ㄤ綔灏嗕笉浼氳瀹屾垚锛
 foreach($models as $i=>$model)
    $model->attribute = CUploadedFile::getInstance($model, "[$i]attribute");
璇锋敞鎰忥細浣犲繀椤讳娇鐢CUploadedFile::getInstances 鏉ュ畬鎴愬鏂囦欢涓婁紶銆

褰撳湪涓涓椿鍔ㄨ褰曚腑浣跨敤CFileValidator锛岄氬父浼氫娇鐢ㄧ被浼间笅闈㈢殑浠g爜锛
 if($model->save())
 {
    // single upload
    $model->attribute->saveAs($path);
    // multiple upload
    foreach($model->attribute as $file)
       $file->saveAs($path);
 }


浣犲彲浠ヤ娇鐢CFileValidator鏉ラ獙璇佹枃浠剁殑灞炴с

闄や簡浣跨敤message灞炴ф潵璁剧疆鑷畾涔夐敊璇彁绀轰俊鎭箣澶栵紝 鏍规嵁涓嶅悓鐨勯獙璇佹儏鏅ā寮忥紝浣犺繕鍙互涓篊FileValidator璁剧疆涓浜涳紝 涓嶅悓鐨勮嚜瀹氫箟閿欒鎻愮ず淇℃伅銆傚鏋滀笂浼犳枃浠跺お澶э紝浣犲彲浠ヤ娇鐢 tooLarge灞炴ф潵瀹氫箟閿欒鎻愮ず淇℃伅銆傜被浼煎湴鏈tooSmallwrongTypetooMany銆傝嚜瀹氫箟鐨勯敊璇彁绀轰俊鎭 鍙互鍖呮嫭鍗犱綅绗︼紝鍗犱綅绗﹀皢琚疄闄呭唴瀹规浛鎹€傞櫎浜嗘墍鏈夐獙璇佸櫒鍧 鍙瘑鍒殑鈥渰attribute}鈥濆崰浣嶇锛堝弬瑙CValidator锛夊锛 CFileValidator杩樺厑璁告寚瀹氫笅鍒楀崰浣嶇锛
  • {file}: 浣跨敤鏂囦欢鍚嶆浛鎹€
  • {limit}: 褰撲娇鐢tooLarge鏃讹紝浣跨敤maxSize 鏇挎崲锛涘綋浣跨敤tooSmall鏃讹紝浣跨敤maxSize鏇挎崲锛涘綋 浣跨敤tooMany鏃讹紝浣跨敤maxFiles鏇挎崲銆
  • {extensions}: 褰撲娇鐢wrongType鏃讹紝浣跨敤琚厑璁哥殑鎵╁睍鍚嶆浛鎹€

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
allowEmpty boolean 姝ゅ睘鎬ф槸鍚﹂渶瑕佷笂浼犱竴涓枃浠躲 榛樿鍊兼槸false锛岃〃绀洪渶瑕佷笂浼犱竴涓枃浠躲 CFileValidator
attributes array 闇瑕佽楠岃瘉鐨勫睘鎬х殑鍒楄〃銆 CValidator
builtInValidators array 鍐呯疆楠岃瘉鍣ㄥ垪琛 (name=>class) CValidator
enableClientValidation boolean 鏄惁鎵ц瀹㈡埛绔獙璇併傞粯璁ゅ间负true銆 鍙傝CActiveForm::enableClientValidation浠ヤ簡瑙f洿澶氬叧浜庡鎴风楠岃瘉鐨勭粏鑺傘 CValidator
maxFiles integer 缁欏畾灞炴у彲澶勭悊鐨勬渶澶ф枃浠舵暟銆 榛樿鍊兼槸1锛岃〃绀哄崟涓枃浠朵笂浼犮傞氳繃璁剧疆涓涓緝楂樼殑鏁板硷紝 鍙互杩涜澶氭枃浠朵笂浼犮 CFileValidator
maxSize integer 涓婁紶鏂囦欢鍏佽鐨勬渶澶у瓧鑺傛暟銆 榛樿鍊间负null锛岃〃绀轰笉闄愬埗銆 娉ㄦ剰锛氬厑璁哥殑鏈澶у瓧鑺傛暟涔熷悓鏃跺彈 'upload_max_filesize' INI 璁剧疆鍊 鍜 'MAX_FILE_SIZE' 闅愯棌瀛楁鍊肩殑褰卞搷銆 CFileValidator
message string 鐢ㄦ埛鑷畾涔夌殑閿欒鎻愮ず淇℃伅銆備笉鍚岀殑楠岃瘉鍣ㄥ彲浠ュ湪璇ヤ俊鎭腑 瀹氫箟鍚勭鍗犱綅绗︼紙灏嗚瀹為檯鍊兼浛鎹級銆傚崰浣嶇鈥渰attribute}鈥濆彲浠ヨ鎵鏈 楠岃瘉鍣ㄨ瘑鍒紝瀹冧細琚娇鐢ㄥ睘鎬х殑鏍囩鏉ユ浛鎹€ CValidator
minSize integer 涓婁紶鏂囦欢鍏佽鐨勬渶灏忓瓧鑺傛暟銆 榛樿鍊间负null锛岃〃绀轰笉闄愬埗銆 CFileValidator
on array 楠岃瘉鍣ㄥ皢琚簲鐢ㄥ埌鐨勬儏鏅ā寮忕殑鍒楄〃銆 鏁扮粍鐨勯敭-鍊奸兘鏄儏鏅ā寮忕殑鍚嶇О銆 CValidator
safe boolean 杩涜鏁村潡璧嬪兼槸鏄惁鑰冭檻姝ら獙璇佸櫒涓垪鍑虹殑灞炴х殑瀹夊叏鎬с 榛樿鍊间负true銆 CValidator
skipOnError boolean 濡傛灉褰撳墠灞炴у凡缁忓瓨鍦ㄩ獙璇侀敊璇紝杩欎釜楠岃瘉瑙勫垯 鏄惁璺宠繃銆傞粯璁ゅ兼槸false銆 CValidator
tooLarge string 涓婁紶鏂囦欢澶ぇ鐨勯敊璇彁绀轰俊鎭 CFileValidator
tooMany string 涓婁紶鏂囦欢鏁拌秴杩囬檺鍒剁殑 閿欒鎻愮ず淇℃伅銆 CFileValidator
tooSmall string 涓婁紶鏂囦欢澶皬鐨勯敊璇彁绀轰俊鎭 CFileValidator
types mixed 鍏佽琚笂浼犵殑鏂囦欢鎵╁睍鍚嶇殑鍒楄〃銆 鍙互鏄竴涓暟缁勶紝涔熷彲浠ユ槸涓涓敱琚厑璁哥殑鏂囦欢鎵╁睍鍚 缁勬垚鐨勫瓧绗︿覆锛屾墿灞曞悕涔嬮棿浣跨敤绌烘牸鎴栭楀彿鍒嗛殧 锛堜緥濡傦細"gif, jpg"锛夈傛枃浠舵墿灞曞悕鏄ぇ灏忓啓鏁忔劅鐨勩 榛樿鍊兼槸null锛岃〃绀烘墍鏈夌被鍨嬮兘琚厑璁搞 CFileValidator
wrongType string 涓婁紶鏂囦欢鐨勬墿灞曞悕涓嶅寘鍚湪extensions 涓殑閿欒鎻愮ず淇℃伅銆 CFileValidator

鍙椾繚鎶ゅ睘鎬

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
sizeLimit integer 杩斿洖鍏佽鐨勪笂浼犳枃浠舵渶澶у瓧鑺傛暟 CFileValidator

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
applyTo() 杩斿洖鍊兼寚瀹氭楠岃瘉鍣ㄦ槸鍚﹀簲鐢ㄥ埌鎸囧畾鐨勬儏鏅ā寮忋 CValidator
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
clientValidateAttribute() 杩斿洖鎵ц瀹㈡埛绔獙璇佹墍闇鐨凧avaScript鑴氭湰銆 CValidator
createValidator() 鍒涘缓涓涓獙璇佸櫒瀵硅薄銆 CValidator
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
validate() 楠岃瘉缁欏畾鐨勫璞° CValidator

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
addError() 娣诲姞鍏充簬鎸囧畾灞炴х殑涓涓敊璇彁绀轰俊鎭埌娲诲姩璁板綍涓 CValidator
emptyAttribute() 閫氱煡缁堢鐢ㄦ埛鍑虹幇浜嗙┖鐧藉睘鎬х殑閿欒 CFileValidator
getSizeLimit() 杩斿洖鍏佽鐨勪笂浼犳枃浠舵渶澶у瓧鑺傛暟 CFileValidator
isEmpty() 妫娴嬬粰瀹氬兼槸鍚︿负绌恒 CValidator
validateAttribute() 涓轰紶鍏ュ璞$殑灞炴ц祴鍊硷紝鐒跺悗浣跨敤validateFile杩涜楠岃瘉銆 CFileValidator
validateFile() 鍐呴儴楠岃瘉涓涓枃浠跺璞° CFileValidator

灞炴ц缁

allowEmpty 灞炴
public boolean $allowEmpty;

姝ゅ睘鎬ф槸鍚﹂渶瑕佷笂浼犱竴涓枃浠躲 榛樿鍊兼槸false锛岃〃绀洪渶瑕佷笂浼犱竴涓枃浠躲

maxFiles 灞炴
public integer $maxFiles;

缁欏畾灞炴у彲澶勭悊鐨勬渶澶ф枃浠舵暟銆 榛樿鍊兼槸1锛岃〃绀哄崟涓枃浠朵笂浼犮傞氳繃璁剧疆涓涓緝楂樼殑鏁板硷紝 鍙互杩涜澶氭枃浠朵笂浼犮

maxSize 灞炴
public integer $maxSize;

涓婁紶鏂囦欢鍏佽鐨勬渶澶у瓧鑺傛暟銆 榛樿鍊间负null锛岃〃绀轰笉闄愬埗銆 娉ㄦ剰锛氬厑璁哥殑鏈澶у瓧鑺傛暟涔熷悓鏃跺彈 'upload_max_filesize' INI 璁剧疆鍊 鍜 'MAX_FILE_SIZE' 闅愯棌瀛楁鍊肩殑褰卞搷銆

鍙傝

minSize 灞炴
public integer $minSize;

涓婁紶鏂囦欢鍏佽鐨勬渶灏忓瓧鑺傛暟銆 榛樿鍊间负null锛岃〃绀轰笉闄愬埗銆

鍙傝

sizeLimit 灞炴 鍙
protected integer getSizeLimit()

杩斿洖鍏佽鐨勪笂浼犳枃浠舵渶澶у瓧鑺傛暟 鏈澶у瓧鑺傛暟鐢变互涓嬩笁涓洜绱犲叡鍚屽喅瀹氾細

tooLarge 灞炴
public string $tooLarge;

涓婁紶鏂囦欢澶ぇ鐨勯敊璇彁绀轰俊鎭

鍙傝

tooMany 灞炴
public string $tooMany;

涓婁紶鏂囦欢鏁拌秴杩囬檺鍒剁殑 閿欒鎻愮ず淇℃伅銆

tooSmall 灞炴
public string $tooSmall;

涓婁紶鏂囦欢澶皬鐨勯敊璇彁绀轰俊鎭

鍙傝

types 灞炴
public mixed $types;

鍏佽琚笂浼犵殑鏂囦欢鎵╁睍鍚嶇殑鍒楄〃銆 鍙互鏄竴涓暟缁勶紝涔熷彲浠ユ槸涓涓敱琚厑璁哥殑鏂囦欢鎵╁睍鍚 缁勬垚鐨勫瓧绗︿覆锛屾墿灞曞悕涔嬮棿浣跨敤绌烘牸鎴栭楀彿鍒嗛殧 锛堜緥濡傦細"gif, jpg"锛夈傛枃浠舵墿灞曞悕鏄ぇ灏忓啓鏁忔劅鐨勩 榛樿鍊兼槸null锛岃〃绀烘墍鏈夌被鍨嬮兘琚厑璁搞

wrongType 灞炴
public string $wrongType;

涓婁紶鏂囦欢鐨勬墿灞曞悕涓嶅寘鍚湪extensions 涓殑閿欒鎻愮ず淇℃伅銆

鏂规硶璇︾粏

emptyAttribute() 鏂规硶
protected void emptyAttribute(CModel $object, string $attribute)
$object CModel 闇瑕侀獙璇佺殑瀵硅薄
$attribute string 闇瑕侀獙璇佺殑灞炴
婧愮爜锛 framework/validators/CFileValidator.php#204 (鏄剧ず)
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);
    }
}

閫氱煡缁堢鐢ㄦ埛鍑虹幇浜嗙┖鐧藉睘鎬х殑閿欒

getSizeLimit() 鏂规硶
protected integer getSizeLimit()
{return} integer 涓婁紶鏂囦欢鐨勫瓧鑺傛暟銆
婧愮爜锛 framework/validators/CFileValidator.php#224 (鏄剧ず)
protected function getSizeLimit()
{
    
$limit=ini_get('upload_max_filesize');
    
$limit=$this->sizeToBytes($limit);
    if(
$this->maxSize!==null && $limit>&& $this->maxSize<$limit)
        
$limit=$this->maxSize;
    if(isset(
$_POST['MAX_FILE_SIZE']) && $_POST['MAX_FILE_SIZE']>&& $_POST['MAX_FILE_SIZE']<$limit)
        
$limit=$_POST['MAX_FILE_SIZE'];
    return 
$limit;
}

杩斿洖鍏佽鐨勪笂浼犳枃浠舵渶澶у瓧鑺傛暟 鏈澶у瓧鑺傛暟鐢变互涓嬩笁涓洜绱犲叡鍚屽喅瀹氾細

validateAttribute() 鏂规硶
protected void validateAttribute(CModel $object, string $attribute)
$object CModel 闇瑕侀獙璇佺殑瀵硅薄
$attribute string 闇瑕侀獙璇佺殑灞炴
婧愮爜锛 framework/validators/CFileValidator.php#124 (鏄剧ず)
protected function validateAttribute($object$attribute)
{
    if(
$this->maxFiles 1)
    {
        
$files=$object->$attribute;
        if(!
is_array($files) || !isset($files[0]) || !$files[0] instanceof CUploadedFile)
            
$files CUploadedFile::getInstances($object$attribute);
        if(array()===
$files)
            return 
$this->emptyAttribute($object$attribute);
        if(
count($files) > $this->maxFiles)
        {
            
$message=$this->tooMany!==null?$this->tooMany Yii::t('yii''{attribute} cannot accept more than {limit} files.');
            
$this->addError($object$attribute$message, array('{attribute}'=>$attribute'{limit}'=>$this->maxFiles));
        }
        else
            foreach(
$files as $file)
                
$this->validateFile($object$attribute$file);
    }
    else
    {
        
$file $object->$attribute;
        if(!
$file instanceof CUploadedFile)
        {
            
$file CUploadedFile::getInstance($object$attribute);
            if(
null===$file)
                return 
$this->emptyAttribute($object$attribute);
        }
        
$this->validateFile($object$attribute$file);
    }
}

涓轰紶鍏ュ璞$殑灞炴ц祴鍊硷紝鐒跺悗浣跨敤validateFile杩涜楠岃瘉銆 濡傛灉瀛樺湪浠讳綍楠岃瘉閿欒锛岄敊璇彁绀轰俊鎭皢琚坊鍔犲埌瀵硅薄涓

validateFile() 鏂规硶
protected void validateFile(CModel $object, string $attribute, CUploadedFile $file)
$object CModel 闇瑕侀獙璇佺殑瀵硅薄
$attribute string 闇瑕侀獙璇佺殑灞炴
$file CUploadedFile 涓婁紶鐨勬枃浠讹紝鍩轰簬涓涓鍒欑殑闆嗗悎瀵瑰叾杩涜妫娴
婧愮爜锛 framework/validators/CFileValidator.php#161 (鏄剧ず)
protected function validateFile($object$attribute$file)
{
    if(
null===$file || ($error=$file->getError())==UPLOAD_ERR_NO_FILE)
        return 
$this->emptyAttribute($object$attribute);
    else if(
$error==UPLOAD_ERR_INI_SIZE || $error==UPLOAD_ERR_FORM_SIZE || $this->maxSize!==null && $file->getSize()>$this->maxSize)
    {
        
$message=$this->tooLarge!==null?$this->tooLarge Yii::t('yii','The file "{file}" is too large. Its size cannot exceed {limit} bytes.');
        
$this->addError($object,$attribute,$message,array('{file}'=>$file->getName(), '{limit}'=>$this->getSizeLimit()));
    }
    else if(
$error==UPLOAD_ERR_PARTIAL)
        throw new 
CException(Yii::t('yii','The file "{file}" was only partially uploaded.',array('{file}'=>$file->getName())));
    else if(
$error==UPLOAD_ERR_NO_TMP_DIR)
        throw new 
CException(Yii::t('yii','Missing the temporary folder to store the uploaded file "{file}".',array('{file}'=>$file->getName())));
    else if(
$error==UPLOAD_ERR_CANT_WRITE)
        throw new 
CException(Yii::t('yii','Failed to write the uploaded file "{file}" to disk.',array('{file}'=>$file->getName())));
    else if(
defined('UPLOAD_ERR_EXTENSION') && $error==UPLOAD_ERR_EXTENSION)  // available for PHP 5.2.0 or above
        
throw new CException(Yii::t('yii','File upload was stopped by extension.'));

    if(
$this->minSize!==null && $file->getSize()<$this->minSize)
    {
        
$message=$this->tooSmall!==null?$this->tooSmall Yii::t('yii','The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.');
        
$this->addError($object,$attribute,$message,array('{file}'=>$file->getName(), '{limit}'=>$this->minSize));
    }

    if(
$this->types!==null)
    {
        if(
is_string($this->types))
            
$types=preg_split('/[\s,]+/',strtolower($this->types),-1,PREG_SPLIT_NO_EMPTY);
        else
            
$types=$this->types;
        if(!
in_array(strtolower($file->getExtensionName()),$types))
        {
            
$message=$this->wrongType!==null?$this->wrongType Yii::t('yii','The file "{file}" cannot be uploaded. Only files with these extensions are allowed: {extensions}.');
            
$this->addError($object,$attribute,$message,array('{file}'=>$file->getName(), '{extensions}'=>implode(', ',$types)));
        }
    }
}

鍐呴儴楠岃瘉涓涓枃浠跺璞°

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.