Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

To my misbelief, there is no such module. Is there any module or method to calculate the file size of any file extention in KB? I want to setup an upload form so users can upload .docs, .txts, .images, .exes, and I want to determine what the file size is.

Replies are listed 'Best First'.
•Re: File::Size
by merlyn (Sage) on Jan 14, 2004 at 01:37 UTC
Re: File::Size
by jweed (Chaplain) on Jan 14, 2004 at 01:38 UTC
Re: File::Size
by Zaxo (Archbishop) on Jan 14, 2004 at 01:49 UTC

    That hardly needs a module when there is the -s operator,

    $ perl -e'printf "%s %.0fK\n", $_, (-s)/1024 for <*.png>' 2002_04_16_035041_shot.png 7K 2002_04_16_035143_shot.png 2K 2002_04_24_194156_shot.png 21K 2002_04_24_194408_shot.png 20K 2002_04_24_195557_shot.png 22K 2002_04_24_202442_shot.png 17K noise.png 7K pm_shot.png 7K unix_time.png 376K Zaxo_nn_shot2.png 9K zaxochair.png 26K $

    It sounds, though, like you want the server to read file sizes on the client machine. You can't do that over http. You need a client willing to run your javascript or whatever to do that.

    After Compline,
    Zaxo

Re: File::Size
by l3nz (Friar) on Jan 14, 2004 at 13:40 UTC
    If it's not -s, you probably meant to know: how do I know a file size in advance when someone uploads a file?

    You usually have no way of controlling that, though your web server will put a maximum length to the size of the file it's receiving. You will simply evaluate the length of the returned file (you need a module to parse it, I believe there must be something in the CGI family) and then decide what to do with the uploaded file.

Re: File::Size
by Roger (Parson) on Jan 14, 2004 at 01:38 UTC
    TIMTOWTDI, with seek and tell...
    use IO::File; ... sub getfilesize { my $filename = shift; my $f = new IO::File $filename or die "File not found"; seek $f, 0, 2; return tell $f; }
      There's no promise that the return value from tell will be the byte offset, only that it can be used with seek to go back to the same place. On some systems, it may need to be "a magic cookie" that has the record and line number encoded opaquely, for example.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Thanks for the tip. ;-)

    A reply falls below the community's threshold of quality. You may see it by logging in.