Fixed #5033 - use PHP’s max filesize for image uploads

This commit is contained in:
snipe 2018-02-16 21:17:41 -08:00
parent ba5057181e
commit d8fc50c351
9 changed files with 87 additions and 9 deletions

View file

@ -731,5 +731,62 @@ class Helper
} }
// Nicked from Drupal :)
// Returns a file size limit in bytes based on the PHP upload_max_filesize
// and post_max_size
public static function file_upload_max_size() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = Helper::parse_size(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = Helper::parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
public static function file_upload_max_size_readable() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = Helper::parse_size(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = ini_get('post_max_size');
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = Helper::parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = ini_get('upload_max_filesize');
}
}
return $max_size;
}
public static function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
} }

Binary file not shown.

BIN
public/js/dist/all.js vendored

Binary file not shown.

View file

@ -8,7 +8,7 @@
"/css/app.css.map": "/css/app.css.map?id=bdbe05e6ecd70ccfac72", "/css/app.css.map": "/css/app.css.map?id=bdbe05e6ecd70ccfac72",
"/css/overrides.css.map": "/css/overrides.css.map?id=898c91d4a425b01b589b", "/css/overrides.css.map": "/css/overrides.css.map?id=898c91d4a425b01b589b",
"/css/dist/all.css": "/css/dist/all.css?id=dc1449877e0f8abedc47", "/css/dist/all.css": "/css/dist/all.css?id=dc1449877e0f8abedc47",
"/js/dist/all.js": "/js/dist/all.js?id=4e5e7295e9a59e718567", "/js/dist/all.js": "/js/dist/all.js?id=d3af8331997bd82e4ec4",
"/css/build/all.css": "/css/build/all.css?id=dc1449877e0f8abedc47", "/css/build/all.css": "/css/build/all.css?id=dc1449877e0f8abedc47",
"/js/build/all.js": "/js/build/all.js?id=4e5e7295e9a59e718567" "/js/build/all.js": "/js/build/all.js?id=d3af8331997bd82e4ec4"
} }

View file

@ -322,6 +322,25 @@ $(document).ready(function () {
// ------------------------------------------------ // ------------------------------------------------
// File size validation
$('#uploadFile').bind('change', function() {
$('#upload-file-status').removeClass('text-success').removeClass('text-danger');
$('.goodfile').remove();
$('.badfile').remove();
var max_size = $('#uploadFile').data('maxsize');
var actual_size = this.files[0].size;
if (actual_size > max_size) {
$('#upload-file-status').addClass('text-danger').removeClass('help-block').prepend('<i class="badfile fa fa-times"></i> ');
} else {
$('#upload-file-status').addClass('text-success').removeClass('help-block').prepend('<i class="goodfile fa fa-check"></i> ');
}
$('#upload-file-info').html(this.files[0].name);
});

View file

@ -97,7 +97,7 @@
'image' => 'Image', 'image' => 'Image',
'image_delete' => 'Delete Image', 'image_delete' => 'Delete Image',
'image_upload' => 'Upload Image', 'image_upload' => 'Upload Image',
'image_filetypes_help' => 'Accepted filetypes are jpg, png, gif, and svg.', 'image_filetypes_help' => 'Accepted filetypes are jpg, png, gif, and svg. Max upload size allowed is :size.',
'import' => 'Import', 'import' => 'Import',
'import-history' => 'Import History', 'import-history' => 'Import History',
'asset_maintenance' => 'Asset Maintenance', 'asset_maintenance' => 'Asset Maintenance',

View file

@ -823,5 +823,7 @@
</script> </script>
@endif @endif
</body> </body>
</html> </html>

View file

@ -1,13 +1,13 @@
<div class="form-group {{ $errors->has('image') ? 'has-error' : '' }}"> <div class="form-group {{ $errors->has('image') ? 'has-error' : '' }}">
<label class="col-md-3 control-label" for="image">{{ trans('general.image_upload') }}</label> <label class="col-md-3 control-label" for="image">{{ trans('general.image_upload') }}</label>
<div class="col-md-5"> <div class="col-md-9">
<label class="btn btn-default"> <label class="btn btn-default">
{{ trans('button.select_file') }} {{ trans('button.select_file') }}
<input type="file" name="image" accept="image/gif,image/jpeg,image/png,image/svg" <input type="file" name="image" id="uploadFile" data-maxsize="{{ \App\Helpers\Helper::file_upload_max_size() }}" accept="image/gif,image/jpeg,image/png,image/svg" style="display:none">
onchange="$('#upload-file-info').html(this.files[0].name)" style="display:none">
</label> </label>
<span class='label label-default' id="upload-file-info"></span> <span class='label label-default' id="upload-file-info"></span>
<p class="help-block">{{ trans('general.image_filetypes_help') }}</p>
<p class="help-block" id="upload-file-status">{{ trans('general.image_filetypes_help', ['size' => \App\Helpers\Helper::file_upload_max_size_readable()]) }}</p>
{!! $errors->first('image', '<span class="alert-msg">:message</span>') !!} {!! $errors->first('image', '<span class="alert-msg">:message</span>') !!}
</div> </div>
</div> </div>

View file

@ -1,4 +1,4 @@
<div class="box-footer text-right"> <div class="box-footer text-right">
<a class="btn btn-link" href="{{ URL::previous() }}">{{ trans('button.cancel') }}</a> <a class="btn btn-link text-left" href="{{ URL::previous() }}">{{ trans('button.cancel') }}</a>
<button type="submit" class="btn btn-success"><i class="fa fa-check icon-white"></i> {{ trans('general.save') }}</button> <button type="submit" class="btn btn-success"><i class="fa fa-check icon-white"></i> {{ trans('general.save') }}</button>
</div> </div>