Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display upload file size limit on file upload form #6355

Closed
jrtilson opened this issue Jan 23, 2014 · 6 comments
Closed

Display upload file size limit on file upload form #6355

jrtilson opened this issue Jan 23, 2014 · 6 comments

Comments

@jrtilson
Copy link
Contributor

I've implemented this in a plugin, but I'm thinking this would be useful in the core files plugin.

It's easy enough to get the upload/post max sizes in bytes, ie:

elgg_get_ini_setting_in_bytes('upload_max_filesize');
elgg_get_ini_setting_in_bytes('post_max_size');

The only missing piece of the puzzle is a function to convert bytes to a human
readable/friendly string. There's a ton of different ways to go about this..

I like this method (from: http://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes):

function formatBytes($size, $precision = 2) {
    $base = log($size) / log(1024);
    $suffixes = array('', 'k', 'M', 'G', 'T');   

    return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}

If anyone has a better solution for friendly byte formatting, please comment. I'll work this into a pull request otherwise..

@mrclay
Copy link
Member

mrclay commented Jan 23, 2014

Seems fine. Display this in its own view/extension so site owners can easily remove it if they like.

@jrtilson
Copy link
Contributor Author

Few logistical questions:

  1. Should I put this in master, or the 1.8 branch?
  2. Should the bytes formatting function live in lib/output, ie: elgg_format_bytes
  3. Is it worth writing an authoritative function in core to determine the actual file upload limit? upload_max_filesize could be greater than post_max_size (on a misconfigured install)

@Srokap
Copy link
Contributor

Srokap commented Jan 23, 2014

The code for formatBytes is from php.net, licensed under CC license and AFAIK it's not compatible with MIT. I think we just need to write custom one.

@mrclay
Copy link
Member

mrclay commented Jan 23, 2014

  1. master
  2. elgg_format_bytes in lib/output seems fine
  3. Not sure

Do units like KB/MB need to be translated?

@jrtilson
Copy link
Contributor Author

@Srokap The formatBytes function I referenced is actually the second answer to that SO question (http://stackoverflow.com/a/2510540/1202510)

I've modified it to return false to address the error if you pass 0 or a negative number, as well as using proper (I believe) standard symbols for kB, MB etc:

function elgg_format_bytes($size, $precision = 2) {
    if (!$size || $size < 0) {
        return false;
    }

    $base = log($size) / log(1024);
    $suffixes = array('B', 'kB', 'MB', 'GB', 'TB');   

    return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
}

@mrclay I suppose it'd be more flexible to handle any file size unit to/from any other unit size, but that seems a bit beyond the scope of this feature.

@juho-jaakkola
Copy link
Member

Already merged to 1.x, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants