Files limits in folder

I encountered with small WP problem. I want to limit one folder (images/avatars), that it should not go to upload file biggest than 100KB. How I can limit this folder? Thanks!

Related posts

Leave a Reply

1 comment

  1. As you have not posted any code or your effort , let assume example this is code to handle your media post! in WP.

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
    $attachment_id = media_handle_upload('file-upload', $post->ID);
    

    In my form:

    <input type="file" name="file-upload" id="file-upload" />

    As far as I know, WordPress has nothing built in for this, I would just do:

    filesize( get_attached_file( $attachment->ID ) );
    

    Or create a custom function

    function getSize($file){
    $bytes = filesize($file);
    $s = array('b', 'Kb', 'Mb', 'Gb');
    $e = floor(log($bytes)/log(1024));
    return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));}
    

    Although WP has two of them built into core. size_format() and
    wp_convert_bytes_to_hr()

    to calculate file size and then if size is greater than 100K and uploaded folder is images/avatars… you can drop user request with some error message!