michellem has asked for the wisdom of the Perl Monks concerning the following question:
I've been struggling with this for a while, and I'm not sure what I've been doing wrong. I've read a number of nodes, including Ovid's nice treatise on file uploads. I've looked at the camel book, as well as read perldoc on CGI. I've modified this script lots over the last few days, but I'm still stumped.
Primarily, my problem is that the files that are uploaded are empty. I also have an unrelated problem, with passing any information except a file name using a multi-part form (thus I've had to do the obnoxious work-around of writing to a file with the directory I want the file uploaded in.) There are several problems with this script, I know. For one, it does not pass strict (I can fix that later, I just want to get the upload working now). Any advice on what I am doing wrong would be greatly appreciated!
Here's the code:
#!/usr/bin/perl -w -T use CGI ':all'; use Fcntl; # use strict; my $q = new CGI; print header, start_html('file upload'), h1('file upload'); my $dir; if ($q->param('file_upload')) { $dir = print_results(); print $q->a({href=>"xi_fileshare.cgi?directory=$dir"},"Back to fil +e listing"); exit; } else {print_form();} print end_html; exit; sub print_form { # print $q->param('directory'); open (FILE,">xi_fileshare.dir") or graceful_exit("Can't write to d +irectory file"); my $directory = $q->param('directory'); print FILE $directory; close FILE; print $q->br; print $q->start_multipart_form(); print $q->filefield(-name=>'file_upload',-size=>60); print $q->br; print $q->submit(-label=>'Upload File'); print $q->end_form; } sub print_results { use constant BUFFER_SIZE => 16384; $CGI::DISABLE_UPLOADS = 0; open (FILE, "xi_fileshare.dir") or graceful_exit("Can't file direc +tory file!"); my $directory = <FILE>; print "Dir:$directory"; my $length; my $file = $q->param('file_upload'); if (!$file) {graceful_exit("No File!");} print h2('File name'),$file; print h2('File Mime Type'),$q->uploadInfo($file)->{'Content-Type'} +; while (<$file>) { $length += length($_); } print h2('File Length'),$length; # OK, Upload that file my $buffer; my $file_handle = $q->upload($file); my $format = $q->uploadInfo($file)->{'Content-Type'}; my $testfilename="whatever.xls"; print "$directory/$testfilename"; print $q-> br; sysopen (OUTFILE, "$directory/$testfilename", O_CREAT) or graceful +_exit("Can't create file!"); while ( read( $file_handle, $buffer, BUFFER_SIZE ) ) { print OUTFILE $buffer; } close (OUTFILE); } sub graceful_exit { my $err = shift; print $q->h3("Sorry, but an error in your input has occured! If yo +u can figure it out, this is it:$err"); print "Use your browser's <b>BACK</b> button and try again with ch +anged input"; print $q->br; print $q->end_html; exit; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: CGI Uploads, again!
by Ovid (Cardinal) on Oct 15, 2001 at 19:01 UTC | |
by michellem (Friar) on Oct 15, 2001 at 19:37 UTC | |
by michellem (Friar) on Oct 16, 2001 at 02:59 UTC | |
by Ovid (Cardinal) on Oct 16, 2001 at 03:15 UTC | |
|
Re: CGI Uploads, again!
by Hero Zzyzzx (Curate) on Oct 15, 2001 at 18:42 UTC | |
by michellem (Friar) on Oct 15, 2001 at 18:54 UTC | |
by cfreak (Chaplain) on Oct 15, 2001 at 19:22 UTC | |
by michellem (Friar) on Oct 15, 2001 at 19:40 UTC |