SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jquery or Javascript Minimum file upload script

    Hi,

    Does anyone have/ know a script that stops a user uploading a file that's size is below 300kb?

    I've been searching around and all I can find is maximum file size scripts.

    Thanks in advance.

    Connor

  2. #2
    SitePoint Enthusiast bronze trophy \\.\'s Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    72
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You would check using your server-side the file size and reject if it was not big enough. PHP has a function called filesize() that returns a file size in bytes.

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,876
    Mentioned
    208 Post(s)
    Tagged
    12 Thread(s)
    You can do this in modern browsers in JS using HTML5's File API.

    Given this:

    Code:
    <input type="file" id="file">
    You can do:

    Code JavaScript:
    var uploadField = document.getElementById("file");
     
    uploadField.onchange = function() {
        if(this.files[0].size > 307200){
           alert("File is too big!");
           this.value = "";
        };
    };

    Fiddle

    https://developer.mozilla.org/en/DOM/File

    Quote Originally Posted by \\.\ View Post
    You would check using your server-side the file size and reject if it was not big enough. PHP has a function called filesize() that returns a file size in bytes.
    Unless I'm mistaken (which is always possible ) this means that the file has to be uploaded before you can check the size.

    As with form validation, I would implement a check on the client side for those browsers that understand it, as well as a check on the server (as \\.\ suggests) for older browsers, or users with JS disabled.

  4. #4
    SitePoint Enthusiast bronze trophy \\.\'s Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    72
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yep, the filesize() function of PHP has to have the file uploaded, it is not possible to obtain the filesize from the browser, it is possible to set the Maximum file size with the use of an input field, that has to be before the actual input field for the file selection.

    Being reliant on client side checks has never been a good idea but it is a good way of filtering because the majority of users wouldn't know how to work around that unless they know how to disable JavaScript in the browser.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •