You deserve a medal for this post. This should definitely make it into the core, as file uploading is an incredibly frustrating experience.
Your return_bytes was not posted, so I used the following functions to return bytes from human readable signifiers:
public static function return_bytes($str) {
$bytes = 0;
$bytes_array = array(
'B' => 1,
'KB' => 1024,
'MB' => 1024 * 1024,
'M' => 1024 * 1024,
'GB' => 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024,
'PB' => 1024 * 1024 * 1024 * 1024 * 1024,
);
$bytes = floatval($str);
if (preg_match('#([KMGTP]?B|[M])$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) {
$bytes *= $bytes_array[$matches[1]];
}
$bytes = intval(round($bytes, 2));
return $bytes;
}
seb, on 11 April 2012 - 04:21 AM, said:
Even if you set the post_max_size to some large value there is a possibility that user will upload even bigger file. Also not any hosting allows to change post_max_size.
There is a possibility to check whether too large file was uploaded and display error:
class XUtils {
/**
* Returns max post size from "php.ini"
* @return integer max_post_size
*/
public static function post_max_size() {
return self::return_bytes(ini_get('post_max_size'));
}
/**
* Test request to big file (more post_max_size)
* @return bool has big file or no
*/
public static function hasBigFile() {
if (isset($_SERVER['CONTENT_TYPE'])) {
$hasContent = preg_match("/^multipart\/form-data; boundary=/",$_SERVER['CONTENT_TYPE']);
$hasContent = $hasContent && isset($_SERVER['CONTENT_LENGTH']) && (
$_SERVER['CONTENT_LENGTH'] > self::post_max_size()
);
return $hasContent;
} else {
return false;
}
}
}
Now you can call XUtils::hasBigFile() and raise exception or display validation error.
@mdomba Maybe such check should be in the yii's CFileValidator?