Better handling of max file upload size according to PHP settings

- Allow unlimited size
- Better parsing of PHP size

Fixes #4896
This commit is contained in:
Frédéric Guillot
2023-03-02 20:24:12 -08:00
committed by Frédéric Guillot
parent 6e84f41517
commit b138a99ce3
11 changed files with 56 additions and 31 deletions

View File

@@ -21,7 +21,7 @@ class ProjectFileController extends BaseController
$this->response->html($this->template->render('project_file/create', array(
'project' => $project,
'max_size' => $this->helper->text->phpToBytes(get_upload_max_size()),
'max_size' => get_upload_max_size(),
)));
}

View File

@@ -40,7 +40,7 @@ class TaskFileController extends BaseController
$this->response->html($this->template->render('task_file/create', array(
'task' => $task,
'max_size' => $this->helper->text->phpToBytes(get_upload_max_size()),
'max_size' => get_upload_max_size(),
)));
}

View File

@@ -71,29 +71,6 @@ class TextHelper extends Base
return round(pow(1024, $base - floor($base)), $precision).$suffixes[(int)floor($base)];
}
/**
* Get the number of bytes from PHP size
*
* @param integer $val PHP size (example: 2M)
* @return integer
*/
public function phpToBytes($val)
{
$size = (int) substr($val, 0, -1);
$last = strtolower(substr($val, -1));
switch ($last) {
case 'g':
$size *= 1024;
case 'm':
$size *= 1024;
case 'k':
$size *= 1024;
}
return $size;
}
/**
* Return true if needle is contained in the haystack
*

View File

@@ -9,7 +9,7 @@
'labelDropzone' => t('Drag and drop your files here'),
'labelOr' => t('or'),
'labelChooseFiles' => t('choose files'),
'labelOversize' => t('The maximum allowed file size is %sB.', $this->text->bytes($max_size)),
'labelOversize' => $max_size > 0 ? t('The maximum allowed file size is %sB.', $this->text->bytes($max_size)) : null,
'labelSuccess' => t('All files have been uploaded successfully.'),
'labelCloseSuccess' => t('Close this window'),
'labelUploadError' => t('Unable to upload this file.'),

View File

@@ -9,7 +9,7 @@
'labelDropzone' => t('Drag and drop your files here'),
'labelOr' => t('or'),
'labelChooseFiles' => t('choose files'),
'labelOversize' => t('The maximum allowed file size is %sB.', $this->text->bytes($max_size)),
'labelOversize' => $max_size > 0 ? t('The maximum allowed file size is %sB.', $this->text->bytes($max_size)) : null,
'labelSuccess' => t('All files have been uploaded successfully.'),
'labelCloseSuccess' => t('Close this window'),
'labelUploadError' => t('Unable to upload this file.'),

View File

@@ -13,7 +13,9 @@
<?= $this->form->label(t('CSV File'), 'file') ?>
<?= $this->form->file('file', $errors) ?>
<?php if ($max_size > 0): ?>
<p class="form-help"><?= t('Maximum size: ') ?><?= is_integer($max_size) ? $this->text->bytes($max_size) : $max_size ?></p>
<?php endif ?>
<?= $this->modal->submitButtons(array('submitLabel' => t('Import'))) ?>
</form>

View File

@@ -30,7 +30,9 @@
<?= $this->form->label(t('CSV File'), 'file') ?>
<?= $this->form->file('file', $errors) ?>
<?php if ($max_size > 0): ?>
<p class="form-help"><?= t('Maximum size: ') ?><?= is_integer($max_size) ? $this->text->bytes($max_size) : $max_size ?></p>
<?php endif ?>
<?= $this->modal->submitButtons(array('submitLabel' => t('Import'))) ?>
</form>

View File

@@ -212,7 +212,44 @@ function build_app_version($ref, $commit_hash)
*/
function get_upload_max_size()
{
return min(ini_get('upload_max_filesize'), ini_get('post_max_size'));
$upload_max_filesize = convert_php_size_to_bytes(ini_get('upload_max_filesize'));
$post_max_size = convert_php_size_to_bytes(ini_get('post_max_size'));
if ($post_max_size == 0) {
return $upload_max_filesize;
}
if ($upload_max_filesize == 0) {
return $post_max_size;
}
return min($post_max_size, $upload_max_filesize);
}
/**
* Get the number of bytes from PHP size
*
* @param integer $value PHP size (example: 2M)
* @return integer
*/
function convert_php_size_to_bytes($value)
{
// Remove the non-unit characters from the size
$unit = preg_replace('/[^bkmgtpezy]/i', '', $value);
// Remove the non-numeric characters from the size
$size = preg_replace('/[^0-9\.]/', '', $value);
switch (strtoupper($unit)) {
case 'G':
$size *= 1024;
case 'M':
$size *= 1024;
case 'K':
$size *= 1024;
}
return $size;
}
/**