Skip to content

Commit

Permalink
CFileValidator now works with php.ini's upload_max_filesize strings w…
Browse files Browse the repository at this point in the history
…ith K, G, k, m, g
  • Loading branch information
alexander.makarow committed Apr 6, 2010
1 parent 2169802 commit faa64a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -28,6 +28,7 @@ Version 1.1.2 to be released
- Enh: CTypeValidator now supports checking array data (Qiang)
- Enh: Added CFileHelper::getExtension() (Qiang)
- Enh: Added CModule::hasModule() (Qiang)
- Enh: CFileValidator now works with php.ini's upload_max_filesize strings with K, G, k, m, g (Sam Dark)
- New #1005: added CWinCache (Sam Dark)

Version 1.1.1 March 14, 2010
Expand Down
22 changes: 19 additions & 3 deletions framework/validators/CFileValidator.php
Expand Up @@ -208,12 +208,28 @@ protected function emptyAttribute($object, $attribute)
protected function getSizeLimit()
{
$limit=ini_get('upload_max_filesize');
if(strpos($limit,'M')!==false)
$limit=$limit*1024*1024;
$limit=$this->sizeToBytes($limit);
if($this->maxSize!==null && $limit>0 && $this->maxSize<$limit)
$limit=$this->maxSize;
if(isset($_POST['MAX_FILE_SIZE']) && $_POST['MAX_FILE_SIZE']>0 && $_POST['MAX_FILE_SIZE']<$limit)
$limit=$_POST['MAX_FILE_SIZE'];
return $limit;
}
}

/**
* Converts php.ini style size to bytes
*
* @param string $sizeStr
* @return int
*/
private function sizeToBytes($sizeStr)
{
switch (substr($sizeStr, -1))
{
case 'M': case 'm': return (int)$sizeStr * 1048576;
case 'K': case 'k': return (int)$sizeStr * 1024;
case 'G': case 'g': return (int)$sizeStr * 1073741824;
default: return (int)$sizeStr;
}
}
}

0 comments on commit faa64a3

Please sign in to comment.