Skip to content

Instantly share code, notes, and snippets.

@taichunmin
Created June 4, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taichunmin/b4cbf7971ce157a16b87 to your computer and use it in GitHub Desktop.
Save taichunmin/b4cbf7971ce157a16b87 to your computer and use it in GitHub Desktop.
<?php
/*
2012/05/15 taichunmin
用途:
計算出最大可用的檔案上傳大小。 ( 以設定檔中之 upload_max_filesize 和 post_max_size 來判定 )
此函式是參考 phpMyAdmin 寫出
參考:
phpMyAdmin - phpMyAdmin\libraries\Config.class.php (checkUploadSize)
php官方網站 - http://php.net/manual/en/function.ini-get.php
*/
function tai_checkUploadSize_returnBytes($val)
{
if(empty($val))return 0;
$val = trim($val);
preg_match('#([0-9]+)[\s]*([a-z]+)#i', $val, $matches);
$last = '';
if(isset($matches[2]))
$last = $matches[2];
if(isset($matches[1]))
$val = (int) $matches[1];
switch (strtolower($last))
{
case 'g':
case 'gb':
$val *= 1024;
case 'm':
case 'mb':
$val *= 1024;
case 'k':
case 'kb':
$val *= 1024;
}
return (int) $val;
}
function tai_checkUploadSize()
{
global $cfg;
if (! $filesize = ini_get('upload_max_filesize'))
$filesize = "5M";
if(! $postsize = ini_get('post_max_size'))
$postsize = $filesize;
$filesize = tai_checkUploadSize_returnBytes($filesize);
$postsize = tai_checkUploadSize_returnBytes($postsize);
$cfg['MAX_FILE_SIZE'] = min($filesize,$postsize);
}
tai_checkUploadSize();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment