in reply to Re: Re: File Upload - What Next?
in thread File Upload - What Next?

Ok, there WAS a problem with this script, but thanks to various people (you know who you are) it now works. Im sure there are scurity issues and all that but it works:) The only one problem i still have is that i get an internal server error when the file size is too big. Must I do some code checking the MAX FILE size or is that what the $CGI: : POST_MAX line is suppose to do for me? Heres the form......
<FORM ENCTYPE="multipart/form-data" ACTION="the.cgi" METHOD=POST> Var1 : <input type=text name=var1><br> File : <INPUT NAME="file" TYPE="file"><p> <INPUT TYPE="submit" VALUE="Send File"> </FORM>
and heres the script.........
#!/usr/bin/perl use CGI; #SET SOME DEFAULT STUFF ################################################### use constant BUFFER_SIZE => 16_384; use constant MAX_FILE_SIZE => 48_576; $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = MAX_FILE_SIZE; #GET THE VARIABLE AND FILE INFORMATION ################################################### my $query = new CGI; my $filehandle = $query->upload('file'); my $filename = $query->param('file'); my $var1 = $query->param('var1'); #GET FILE NAME BY REMOVING FULL DIRECTORY INFO #eg C:\windows\blah\file.txt ############################################## @pathz = (split(/\\/,$filename)); $fileb = $pathz[$#pathz]; @pathza = (split('/',$fileb)); $filename = $pathza[$#pathza]; #UPLOAD THE FILE TO SELECTED DESTINATION ############################################## open OUTPUT, ">/absolute/path/to/$filename" or die "Can't open: $!"; while (<$filehandle>) { print OUTPUT; } close OUTPUT or die "Can't close: $!"; #DONE ############################################## print "Content-type: text/html\n\n"; print "Sorted!<p>\n"; print "Passed Variable = $var1<p>"; print "File name uploaded was <i>$filename</i><p>\n"; exit;
Thankyou everyone!, I hope that helps alot of other people too!

Replies are listed 'Best First'.
Re: File Upload - Complete Code
by arturo (Vicar) on Jul 06, 2001 at 15:29 UTC

    Your suspicion, mentioned in the CB, was that the file's size was causing the problem. That's pretty likely, because you'll notice that CGI::POST_MAX is set to a pretty low number (that value is in *bytes*, by the way ... so as things stand, you'll only allow 48K total in all POSTed data.)

    Other non-miscellany : rewrite this script to work under use strict, you'll thank yourself for it later.

    There are also some modules that can help you make this script more portable; your "filename parsing" routine in fact only handles Win32-style delimiters => \ , but the standard (i.e. already installed) module File::Basename has robust routines that will handle the delimiters used on most operating systems. You'll need to check the $ENV{USER_AGENT} string to make an educated guess about the operating system the client is using. I do believe KM <-- follow that, by the way ... it's my way of indirectly plugging his book -- has a different strategy for dealing with handling uploads for multiple OSes, but I don't recall off the top of my head what it is.

    As far as controlling the error messages, the strategy I like to use is to define a subroutine (I call it bail usually) that will get called in place of die. That way, if something fails I can intercept the error message and give the user something prettier to look at than a 500 error page. If your script is dying *within* calls to functions that are built-in or imported from modules, the usual strategy is to wrap those calls in a block-style eval, and then to check the value of the special variable $@, which will be set to the value of any error thrown by something within the eval block. A toy example:

    eval { die "You're ugly, and your President dresses funny."; } if ($@) { print "Hmm, got an error message that read \"$@\" ... I wonder why? +\n"; }

    Do a super search on "Exception Handling" and read the eval manual page for more info.

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: File Upload - Complete Code
by Anonymous Monk on Jun 20, 2002 at 08:37 UTC
    thx man, nice scipt and it works