summaryrefslogtreecommitdiff
path: root/includes/file.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/file.inc')
-rw-r--r--includes/file.inc36
1 files changed, 36 insertions, 0 deletions
diff --git a/includes/file.inc b/includes/file.inc
index d234bb9de..0d8f53ea2 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -712,4 +712,40 @@ function file_directory_path() {
return variable_get('file_directory_path', 'files');
}
+/**
+ * Helper function for file_upload_max_size().
+ */
+function _file_convert_to_mb($val){
+ $val = trim($val);
+ $last = strtolower($val[strlen($val) - 1]);
+ switch ($last) {
+ // The 'G' modifier is available since PHP 5.1.0
+ case 'g':
+ $size = $val * 1024;
+ break;
+ case 'k':
+ $size = $val / 1024;
+ break;
+ default:
+ $size = (int) $val;
+ }
+ return $size;
+}
+
+/**
+ * Determine the maximum file upload size by querying the PHP settings.
+ *
+ * @return
+ * A file size limit in MB based on the PHP upload_max_filesize and post_max_size
+ */
+function file_upload_max_size() {
+ static $max_size = -1;
+ if ($max_size < 0) {
+ $upload_max = _file_convert_to_mb(ini_get('upload_max_filesize'));
+ // sanity check- a single upload should not be more than 50% the size limit of the total post
+ $post_max = _file_convert_to_mb(ini_get('post_max_size')) / 2;
+ $max_size = ($upload_max < $post_max) ? $upload_max : $post_max;
+ }
+ return $max_size;
+}