Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Need help with CGI::Upload

by tcf22 (Priest)
on Jul 29, 2003 at 13:27 UTC ( [id://278817]=note: print w/replies, xml ) Need Help??


in reply to Need help with CGI::Upload

I normally use the following.
It works for me pretty well for me.
use CGI; $cgi=new CGI; $file=$cgi->param('file_field'); $newName=&FileUpload($file); sub FileUpload{ my $FileName=shift @_; my $tmpdir ="/your/path"; my $bufferSize=4096; my $limit=10000000;#File Size Limit my ($debug, $FILE, $file, $size, $tmppath, $line, $buffer); $size = 0; if (length($FileName) != 0){ $| = 1; $FILE = $FileName; ($file = $FILE) =~ s/^.*?([^\\\/]+)$/$1/; $tmppath = "$tmpdir\\$file"; open (OUT, ">$tmppath") or print "Error opening $tmppa +th: $!<br>\n"; binmode OUT; while(($line = read ($FileName, $buffer, $bufferSize)) + && ($size < $limit)){ print OUT $buffer; $size += $line; if ($size > $limit){ $err_flag = 1; } } close OUT; $| = 0; $FileName = $file; } else{##Empty File $FileName = ""; } #Upload Error if ($err_flag == 1){ unlink ($tmppath); $FileName = ""; } $FileName; }
Tom

Replies are listed 'Best First'.
(jeffa) 2Re: Need help with CGI::Upload
by jeffa (Bishop) on Jul 29, 2003 at 14:26 UTC
    Oh dear ... reinventing established wheels ... bad ...

    Since you have already loaded CGI.pm ... why not use CGI::upload ... it's a whole lot easier. Here is a short example that you can modify (add error checking, etc.).

    #!/usr/bin/perl -T use strict; use HTML::Entities; use CGI qw(:standard); print header, start_multipart_form, filefield('file'), submit('go'), end_form, ; if (param('go')) { my $fh = upload('file'); print hr, pre( map { encode_entities $_ } <$fh>); }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-24 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found