[Memphis.pm] [Fwd: Re: [GOLUM] CGI: file upload widget processing]

Brock Sides bsides at towery.com
Thu Mar 16 15:15:29 CST 2000


On Wed, 15 Mar 2000, cameron walker wrote:

> 
> > 
> > How do you upload a file using HTML/CGI?
> > 
> > Below are the guts of the HTML I wrote. What do I have to do within the
> > CGI to actually write out the contents of a choosen file onto the disk
> > of the server? So far all I can do is print out the name of the file but
> > none of the contents.
> > 
> > </head>
> > <body>
> >  <FORM  METHOD="POST" ACTION="../cgi-local/test.pl"
> > ENCTYPE="multipart/form-data">
> >   <TABLE  COLS="1" CELLPADDING="0">
> >    <TR>
> >     <TD>
> >      <INPUT NAME="file" TYPE="file">
> >      <INPUT NAME="submit" TYPE="submit" VALUE="Initiate">
> >     </TD>
> >    </TR>
> >   </TABLE>
> >  </FORM>
> > </body>
> > </html>

Do something like this:

#!/usr/bin/perl -wT

use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser);

$tmpdir = '/tmp'; # probably a bad idea

if (param('file')) {
        $filename = param('file');
        $filename =~ /(.*[\\\/:])*(.*)/; # put the filename
					 # minus the path in $2
					 # regex magic makes it work
					 # on windows, mac, or unix
					 # and makes taint checking happy
        $filesuffix = $2;
        binmode $filename; # needed on Win32 for binary files
			   # superflous on a real OS
        open(TMPFILE, ">$tmpdir/$filesuffix") || die "Couldn't open temp
file: $!";
        binmode TMPFILE; # for Win32
        while (<$filename>) {	# automagically created filehandle
                print TMPFILE;
        }
}

print <<FIN;
Content-type: text/plain

Done!
FIN

__END__

In other words, if you "use CGI", any file specified through <INPUT
TYPE=FILE> will have a filehandle automagically associated with it which
you can read from.

--
Brock Sides
Unix Systems Administration
Towery Publishing
bsides at towery.com

----------------------------------------------------------------------------
To unsubscribe, please send email to majordomo at pm.org
with 'unsubscribe memphis-pm-list' in the body of the message.
----------------------------------------------------------------------------




More information about the Memphis-pm mailing list