Posted By

kendsnyder on 08/31/11


Tagged


Versions (?)

Get max upload size as set in php.ini


 / Published in: PHP
 

  1. <?php
  2.  
  3. // TESTED:
  4. function getMaxFileUploadSize() {
  5. $maxPost = ini_get('post_max_size');
  6. if (substr($maxPost,-1) == 'K') $maxPost = substr($maxPost,0,-1) * 1024;
  7. elseif (substr($maxPost,-1) == 'M') $maxPost = substr($maxPost,0,-1) * 1024 * 1024;
  8. elseif (substr($maxPost,-1) == 'G') $maxPost = substr($maxPost,0,-1) * 1024 * 1024 * 1024;
  9. $maxUpload = ini_get('upload_max_filesize');
  10. if (substr($maxUpload,-1) == 'K') $maxUpload = substr($maxUpload,0,-1) * 1024;
  11. elseif (substr($maxUpload,-1) == 'M') $maxUpload = substr($maxUpload,0,-1) * 1024 * 1024;
  12. elseif (substr($maxUpload,-1) == 'G') $maxUpload = substr($maxUpload,0,-1) * 1024 * 1024 * 1024;
  13. $maxFileSize = min($maxPost, $maxUpload);
  14. $maxFileSize = floor($maxFileSize / 1024 / 1024) . 'MB';
  15. return $maxFileSize;
  16. }
  17.  
  18. // UNTESTED
  19. function getMaxFileUploadSize() {
  20. $maxPost = _interpretKMG(ini_get('post_max_size'));
  21. $maxUpload = _interpretKMG(ini_get('upload_max_filesize'));
  22. $maxFileSize = min($maxPost, $maxUpload);
  23. $maxFileSize = floor($maxFileSize / 1024 / 1024) . 'MB';
  24. }
  25.  
  26. function _interpretKMG($text) {
  27. if (preg_match('/^\d+$/', $text) {
  28. return $text;
  29. }
  30. $num = substr($text,0,-1);
  31. $suffix = substr($text,-1);
  32. switch ($suffix) {
  33. case 'G': $num *= 1024;
  34. case 'M': $num *= 1024;
  35. case 'K': $num *= 1024;
  36. }
  37. return $num;
  38. }

Report this snippet  

You need to login to post a comment.