Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to get the sizes of a file when i use CGI.PM to upload it~~?

by swansun (Initiate)
on Jul 30, 2001 at 13:15 UTC ( [id://100819]=perlquestion: print w/replies, xml ) Need Help??

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

i'm using CGI.PM to write a perl script to upload files.i just want to get the size of a uploaded file,how can i do? who can help me? thanks~~~
  • Comment on how to get the sizes of a file when i use CGI.PM to upload it~~?

Replies are listed 'Best First'.
Re: how to get the sizes of a file when i use CGI.PM to upload it~~?
by oakbox (Chaplain) on Jul 30, 2001 at 17:05 UTC
    This is straight from the examples section of the CGI.pm documentation:
    # Process the form if there is a file name entered if (my $file = param('filename')) { my %stats; my $tmpfile=tmpFileName($file); my $mimetype = uploadInfo($file)->{'Content-Type'} || ''; print hr(), h2($file), h3($tmpfile), h4("MIME Type:",em($mimetype)); my($lines,$words,$characters,@words) = (0,0,0,0); while (<$file>) { $lines++; $words += @words=split(/\s+/); $characters += length($_); } close $file; grep($stats{$_}++,param('count')); if (%stats) { print strong("Lines: "),$lines,br if $stats{'count lines'}; print strong("Words: "),$words,br if $stats{'count words'}; print strong("Characters: "),$characters,br if $stats{'count c +haracters'}; } else { print strong("No statistics selected."); } }
    Of course, this is for processing a text file. To get information from an uploaded binary file, I do a stat() on the filehandle.
    $filename = $query->param('uploaded_file'); @stats=stat $filename; $size_of_file=$stats[7];
    Hope this helps!
    Oakbox
    "If what I'm saying doesn't make sense, that's because sense cannot be made, it's something that must be sensed"-J.S. Hall
Re: how to get the sizes of a file when i use CGI.PM to upload it~~?
by Wookie (Beadle) on Jul 30, 2001 at 14:06 UTC
    Net::FTP can allow you to read the size of remote files pretty easily.

    Below is some nice sample code for this I prepared earlier :)
    #!/usr/bin/perl -w use strict; use Net::FTP; my ($server, $user, $pass, $file); my $ftph; my @files; my @list; $server = 'wookie.mcom.com'; $user = 'ukgaryc'; $pass = 'Radio105'; $ftph=Net::FTP->new($server)||die("No connection\n"); $ftph->login($user,$pass)||die("Can't login\n"); @files = $ftph->ls; foreach $file (@files) { my $size; $size = $ftph->size($file); $size ? print "$file is $size bytes\n" : print "Cannot get size of + file $file\n"; } $ftph->quit||die("Failed to disconnect\n"); exit(0);
    Hope this helps :)
    game(Wookie,opponent) eq 'Wookie' ? undef $problem : remove_limbs(arms,opponent);
      The idea is ofcourse to upload files from a browser to a HTTP server and not from an HTTP server to an FTP server, but ofcourse credits for mentioning Net::FTP :))

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: how to get the sizes of a file when i use CGI.PM to upload it~~?
by tachyon (Chancellor) on Jul 30, 2001 at 17:45 UTC

    You guys!!!! KISS

    $size = $ENV{CONTENT_LENGTH}; $too_big = 1024*1024; die "This file is to big!" if $size > $too_big;

    With CGI.pm you can set the max upload file size via the POST_MAX var ie:

    CGI::POST_MAX=1024 # set max upload size to 1kB;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: how to get the sizes of a file when i use CGI.PM to upload it~~?
by miyagawa (Chaplain) on Jul 30, 2001 at 13:47 UTC
    Maybe the file of uploaded file is stored in Content-Length: header, in CGI script you can get it via $ENV{CONTENT_LENGTH}.

    --
    Tatsuhiko Miyagawa
    miyagawa@cpan.org

      Actually $ENV{CONTENT_LENGTH} contains more than just the file data. If you upload multiple files, or have other form fields, using $ENV{CONTENT_LENGTH} is way off. My guess would be either with length or with -s/stat.

      Update: Note that even tho you only send file data thru, the $ENV{CONTENT_LENGTH} still isn't the actual size of the file. Other data is send along, data which CGI novices don't need directly.

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://100819]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2025-04-25 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.