Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How To Upload File Using CGI?

by Anonymous Monk
on Jul 02, 2002 at 15:57 UTC ( [id://178891]=perlquestion: print w/replies, xml ) Need Help??

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

Please help Monks!

As part of a CGI script I'm writing (using of course), I need to print up a form to upload a file. That's the easy part, I just do something like this:
print <<EOP; <form enctype="multipart/form-data" method=post> <input type=hidden name=navigateTo value=upload> <p align=center><input type=file name="file"></p> <p align=center><input type=submit value=Upload></p> EOP

So the user enters some filename like C:\My Documents\somefile.txt and clicks Submit.

But now what do I do to actually get the file?

Replies are listed 'Best First'.
(wil) Re: How To Upload File Using CGI?
by wil (Priest) on Jul 02, 2002 at 16:04 UTC
    If you're using, then take a few minutes to read up on the documentation and specifically, read up on the section labeled Using the File Upload Feature. There is even an example there of how to do it, accompanied by the source code.

    If you're still stuck after reading that chapter, come back and post some sample code or at the very least some evidence of your efforts, and we'll be happy to help further.

    - wil
Re: How To Upload File Using CGI?
by kodo (Hermit) on Jul 02, 2002 at 17:14 UTC
    I'd like to suggest you to use CGI::Upload by rob_au. It's really well written and easy to use, and might prevent you from writing exploitable code. (I've seen some pretty bad upload scripts on a page of a friend last week which could have been exploited pretty easily...). But even with using CGI::Upload you should check your Script carefully and have a look at b0ilers Hacking-CGI and of course CGI-Course by ovid. I just tell you the stuff about security because I have made same errors when I started some months ago so I want to prevent others from doing the same :)

      Thanks for your comments giant.

      For an example of this module in use, see this node in the thread where I originally posted CGI::Upload for discussion. Note however, that the code in the CGI::Upload distribution on CPAN has incorporated a number of bug fixes from the code originally posted in that thread.


Re: How To Upload File Using CGI?
by PerpLexicon (Novice) on Jul 02, 2002 at 17:58 UTC
    use CGI; my $req = new CGI; my $File = $req->param('file'); $File =~ /.*\."?(\w*)"?$/; my $Filename = lc($1); open (FILE, ">./$Filename"); while (read ($File, my $Buffer, 1024)) { print FILE $Buffer; } close FILE; eval("Therory: I have a life"); #returns false :'{
Re: How To Upload File Using CGI?
by trouble (Beadle) on Jul 02, 2002 at 18:31 UTC
    I was working on a similar script recently and found a very good example at this site:

    This web page shows an example of how to upload a file, including both the html code as well as perl code. As you can see, the perl script is very similar to the one suggested by PerpLexicon, but there is some extra information and an explanation which I found helpful.

    I hope this helps!
Re: How To Upload File Using CGI?
by atcroft (Abbot) on Jul 02, 2002 at 17:20 UTC

    I would also suggest you might try the search features of this site as well, such as the general search feature near the top of the page, or Super Search, where you might find things like I need a simple web upload script..

    Hope that helps.

Re: How To Upload File Using CGI?
by neilwatson (Priest) on Jul 02, 2002 at 17:23 UTC
    #!/usr/bin/perl -w # important, this limits the size # of the file to upload. This # prevents people from uploading # large files to slow down your # server: $CGI::POST_MAX=1024 * 100; use strict; use File::Copy; use CGI qw/:standard/; use strict; use warnings; # get filename args my $cgi = new CGI or die "new CGI: $!\n"; # The contents of your file is contained # in this scalar: my $file = $cgi->param('file');
    Neil Watson

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-05-25 23:47 GMT
Find Nodes?
    Voting Booth?

    No recent polls found