Check file size before submit the post action to upload file

Discussion in 'JavaScript' started by shekar.peddi, Mar 21, 2007.

  1. #1
    Hi am trying code givrn below for checking the size of a file.
    But it is working well in IE 6.0.


    But is it not compatible with other browsers plz give a solution can this problem is solved in other browsers

    var oas = new ActiveXObject("Scripting.FileSystemObject");


    <html>
    <head>
    <h2>Check the code below, ActiveX will create sometimes problem.</h2>

    <script type ="text/javascript" >

    function A()
    {
    var oas = new ActiveXObject("Scripting.FileSystemObject");
    var d = document.a.b.value;
    var e = oas.getFile(d);
    var g
    var f = e.size;
    alert(f/1024 + " Kilo-bytes");
    }
    </script>


    </head>
    <body>
    <form name="a">
    <input type="file" name="b">
    <input type="button" name="c" value="SIZE" onClick="A();">
    </form>
    </body>
    </html>
     
    shekar.peddi, Mar 21, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    ActiveX Objects are only supported on IE. And I don't think there's another way to do that with Javascript.
     
    nico_swd, Mar 21, 2007 IP
  3. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #3
    If what you want is to restrict the max file size, then you can use the hidden value MAX_FILE_SIZE.
     
    Aragorn, Mar 21, 2007 IP
  4. weilies

    weilies Peon

    Messages:
    453
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I tried to follow the php manual but cant make it
    <!-- The data encoding type, enctype, MUST be specified as below -->
    <form enctype="multipart/form-data" action="receive.php" method="POST">
        <!-- MAX_FILE_SIZE must precede the file input field -->
        <input type="hidden" name="MAX_FILE_SIZE" value="30" />
        <!-- Name of input element determines name in $_FILES array -->
        Send this file: <input name="userfile" type="file" />
        <input type="submit" value="Send File" />
    </form>
    PHP:
    the form submit to
    <?php 
    
    
    $uploaddir = '/var/www/html/xxx/web/temp/vd';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    
    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Possible file upload attack!\n";
    }
    
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    
    print "</pre>";
    
    ?>
    PHP:
    I still waiting like hell :cool:

    I am sure lots of ppl hoping can check file size without waiting it to fully uploaded




    i even sad when i found this
    http://forum.atlantaphp.org/index.php/m/1215/
     
    weilies, Jan 22, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    You could use a Flash uploader. Flash's file system can get the file sizes.

    As for your PHP script, there should be a slash at the end of the upload path.
     
    nico_swd, Jan 22, 2008 IP
  6. weilies

    weilies Peon

    Messages:
    453
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks! i will try Flash Uploader one day. But i jz wonder how come a powerful PHP cant do that
     
    weilies, Jan 24, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Because PHP is a server side language, and it can't check the file's size without having it uploaded fully to the server.
     
    nico_swd, Jan 25, 2008 IP
  8. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #8
    You can do it with PERL (server side).
    Checking the environment variable: my $data_length = $ENV{'CONTENT_LENGTH'}
    if len is ok, then you can continue with: read(STDIN, $buffer, $data_length);
    if not, just die.
     
    ajsa52, Jan 28, 2008 IP
  9. weilies

    weilies Peon

    Messages:
    453
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Is it Perl language, or PERL(a php function)?
    I cant find PERL function in php manual

    If it's a Perl language, how can i use PHP to run Perl script? Coz my website is coded in PHP

    Thanks
     
    weilies, Jan 28, 2008 IP
  10. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #10
    It's Perl Language.
    Probably on your hosting you can run Perl programs.
    The first line of Perl programs is something like this:

    #! /usr/bin/perl
     
    ajsa52, Jan 28, 2008 IP
  11. weilies

    weilies Peon

    Messages:
    453
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Nono...... not good :( Thanks anyway!
     
    weilies, Jan 29, 2008 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    PHP can do it too, if he was looking for a server-side solution.


    Try finding a Flash or Java (Not Javascript) uploader. That'll be the only way to achieve what you want.
     
    nico_swd, Jan 29, 2008 IP
  13. RussellMD

    RussellMD Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13

    Use $_SERVER['CONTENT_LENGTH'] after trying to upload file. This variable returns size of uploaded file, even if upload failed
     
    RussellMD, Apr 10, 2008 IP
  14. Onlinetest

    Onlinetest Guest

    Messages:
    1
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I have the way to fix for FireFox, but is it not compatible with other browsers(IE, chrome,safari,...)
    <html>
    <head><title>This is count File Size</title></head>
    <body>
    <input id="test" type="File"/>
    <input type="button" onclick="checkFileSize();" value="Check File Size" />
    </body>
    <script type="text/javascript">
    function checkFileSize() {
    var node = document.getElementById('test');
    alert('fileSize = '+node.files[0].fileSize);
    }
    </script>
    </html>
     
    Onlinetest, Dec 2, 2008 IP
    MMJ likes this.
  15. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hmm, interesting feature, thanks for posting.
     
    MMJ, Dec 2, 2008 IP