1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Check file size before upload?

Discussion in 'JavaScript' started by anton-io!, Dec 22, 2004.

  1. #1
    Looking for a client side script that will check the file size of a user selected file (i.e., image) before it is uploaded to the server.

    Possible in Javascript?

    Looking for a Javascript/PHP solution or a simple PHP solution if possible.

    Thanks greatly!
     
    anton-io!, Dec 22, 2004 IP
  2. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #2
    php is all server side scripting, so you are out of luck in that regard. Somebody correct me if I'm wrong, but there is no way to check a file size in php prior to upload. You can set a parameter in your php.ini file that I believe is MAX_UPLOAD or MAX_FILE_UPLOAD to 1 megabyte or another arbitrary number if you want. If someone tried to upload a larger file, they would get an error.

    On the javascript/vbscript side, I believe that you are looking for information that is not available via those scripting languages either.

    Your only option would be java if you wanted to check something client side.

    I could be wrong, though. It's happened before (twice) :).
     
    nevetS, Dec 23, 2004 IP
  3. dustin

    dustin Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Java applets have a pretty strict security policy in place. It prevents them from doing nasty things to the computers of people who view them. So I'm not sure if they would be able to check an image's size.
     
    dustin, Dec 23, 2004 IP
  4. relaxzoolander

    relaxzoolander Peon

    Messages:
    141
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i use an asp script that checks file size.
    .
     
    relaxzoolander, Jan 15, 2005 IP
  5. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    phrozen_ra, Jan 18, 2005 IP
  6. RussellMD

    RussellMD Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi!

    You can use a server-side solution.

    Use $_SERVER['CONTENT_LENGTH'] after trying to upload file. This variable returns size of uploaded file, even if upload failed


    if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>2097152)
    $error = 'Upload FAILED, file is too large !';

    This method work fine for all cases of errors when upload file , even if filesize is greater than MAX_FILE_SIZE, upload_max_filesize or post_max_size (from php.ini)

    Good Luck! && Sorry for my bad english.
     
    RussellMD, Apr 10, 2008 IP
  7. dineshw

    dineshw Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    RussellMD,

    Wont that only check the file size AFTER the file has already been uploaded to the temporary folder? Doesnt that defeat the purpose?

    Dinesh
     
    dineshw, Apr 11, 2008 IP
  8. yangyang

    yangyang Banned

    Messages:
    758
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #8
    JavaScript is not able to do it, it's forbidden to access file system.

    However with VBScript on ActiveX object, you can check for file size with client side scripting. Hope the code below helps:

    HTML:
    1. function A()
    2. {
    3. var oas = new ActiveXObject("Scripting.FileSystemObject");
    4. var d = document.a.b.value;
    5. var e = oas.getFile(d);
    6. var f = e.size;
    7. alert(f + " bytes");
    8. }
    9.  
    10. </head>
    11. <form name="a">
    12. <input type="file" name="b">
    13. <input type="button" name="c" value="SIZE" onClick="A();">
    14. </form>
    15. </body>
    16. </html>
     
    yangyang, Jan 1, 2009 IP
    LogicFlux likes this.
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    activex / vbscript is not cross browser and reliant on security limitations.
    checking server side is daft - if you have a limit of say, 5mb and somebody uploads a 5.2 mb file, it means waiting for the whole upload to complete so that php/asp can say 'too big, try again'.

    http://digitarald.de/project/fancyupload/
    uses mootools and a really tiny flash control - check the demos. achieves desired check and much more, including progress report, multiple uploads etc. flash is likely to be installed for at least 90% of your visitors and the solution is semantic anyway - uses a normal form upload which can work via POST if flash is not there.
     
    dimitar christoff, Jan 1, 2009 IP
  10. redrose4u

    redrose4u Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    <?php

    define("MAX_SIZE",2000000);

    if($_FILES["userfile"]["size"] < MAX_SIZE){

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
    echo "The file has been uploaded";
    } else{
    echo "There was an error uploading the file, please try again!";
    }
    }else{
    echo "There was an error uploading the file, please try again!";

    }

    ?>


    here the code.. i hv used this and found an error....
    in case of file size greater than the limit the application stops and displays white page.. no error... how to fix that problem... any one can help ...


    :(

    thanx
     
    redrose4u, Dec 16, 2009 IP
  11. ranacseruet

    ranacseruet Peon

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    you can do it by javascript's flash extension if flash player plugin is installed. otherwise file have to be uploaded via ajax and then you can return the file size from php response, though i don't like this one...
     
    ranacseruet, Dec 16, 2009 IP
  12. redrose4u

    redrose4u Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    using java script the an error occur "active x object code is undefined" ... so if u have any sample code than plz provide.. thnx
     
    redrose4u, Dec 16, 2009 IP
  13. 1000bech

    1000bech Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I found this on the forums

    --> http://forums.digitalpoint.com/showthread.php?t=1461121


    There is an example on how to do it with Javascript, it works for browsers other than MSIE (tested on Chrome and Firefox, shoud work ok in Safari too). You can mix it with the ActiveX Object to get a cross-browser solution :)
     
    1000bech, May 12, 2010 IP
  14. media143

    media143 Member

    Messages:
    238
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #14
    hey mate; anybody can help me to find Ajax and PHP uploading script. I found a lot of scripts but required CGI. I don't know how to install CGI.
     
    media143, May 14, 2010 IP
  15. s.karthick

    s.karthick Member

    Messages:
    291
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #15
    i also need help in this...any one there !!!
     
    s.karthick, Oct 10, 2010 IP
  16. mastermunj

    mastermunj Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    35
    #16
    Has anyone tried swfupload? That should be the best solution for such cases.
     
    mastermunj, Oct 11, 2010 IP
  17. karanic

    karanic Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    <script language="javascript" type="text/javascript">
    function getFileSize() {
    try {
    var b = document.getElementById('<%=fupload.ClientID %>');
    var f = -1;
    // alert(navigator.appName); // to get the browser name
    var oas;
    try {
    /* ----------------Internet Explorer ---------------- */
    if (navigator.appName == "Microsoft Internet Explorer") {
    oas = new ActiveXObject("Scripting.FileSystemObject");
    var d = b.value;
    var e = oas.getFile(d);
    f = e.size;
    if ((f / (1024 * 1024)) > 2) {
    alert('Please check the file size ');
    return false;
    }
    else {
    return true;
    }
    }
    /* ---------------- Other ---------------- */
    else {
    var filesize = b.files[0].size; // b.files[0].Size;
    alert((filesize / (1024 * 1024)) + " MB");
    if ((filesize / (1024 * 1024)) > 2) {
    alert('Please check the file size ');
    return false;
    }
    else {
    return true;
    }
    // return true;
    }
    }
    catch (err1) {
    alert('Tools -> Internet Options -> choose the Security tab Click the Custom Level button Enable the following settings: Run ActiveX controls and plug-ins Initialize and script ActiveX controls not marked as safe.');
    return false;
    }
    }
    catch (err) {
    alert(err.ToString);
    return false;
    }
    }
    </script>
     
    karanic, Aug 14, 2012 IP