Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to upload a file to a web server?

by Anonymous Monk
on Oct 06, 2000 at 03:28 UTC ( [id://35494]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: uploading images
by cwest (Friar) on Oct 06, 2000 at 03:31 UTC
    I don't think you want to tackle this problem without the CGI module.

    There are tons of examples here of this problem.

    Try CGI Upload.

    Originally posted as a Categorized Answer.

Re: How to upload a file to a web server?
by bradcathey (Prior) on Apr 07, 2005 at 08:49 UTC

    The following uses the upload() function in CGI.pm >= 2.47.

    HTML:

    <form action="cgi-bin/uploader.pl" method="post" enctype="multipart/fo +rm-data">

    Perl:

    my $image = upload_file( "file_to_upload.txt", "../incoming" ); sub upload_file { local $| = 1; my( $filename, $path ) = @_; my $file = $query->upload($filename); # the magic open OUTPUT, "> $path/$filename" or die "$path/$filename - $!"; binmode $file; binmode OUTPUT; my $buffer; while ( read( $file, $buffer, 64*2**10 ) ) { print OUTPUT $buffer; } close OUTPUT; close $file; return( $filename ); }

Re: How do I upload an image?
by larryk (Friar) on Mar 26, 2001 at 15:34 UTC
    A bit late I know but I'm sure someone will find this useful...

    I had to write my own when I found out that my version of CGI.pm didn't handle uploads. This is what I came up with for a M$Windwoes application

    binmode STDIN; read (STDIN, my ($cgidata), $ENV{'CONTENT_LENGTH'}) or doerror("Corrup +t CGI header!"); my ($boundary) = $ENV{'CONTENT_TYPE'} =~ /boundary=(\S+)/; my (@form) = split /\x0d?\x0a?--$boundary-?-?\x0d\x0a/, $cgidata; for (@form) { if ($_ =~ /content-type:/i) { my ($details, $file) = split /\x0d\x0a\x0d\x0a/, $_, 2; $details =~ s/\x0d//gs; my ($MIME) = $details =~ /content-type: (.*)$/i; my ($name) = $details =~ /\sname="([^"]+)/i; if ($MIME =~ /jpe?g/i) { $image{$name} = $file } else { $textfile{$name} = $file } } else { my ($element, $value) = split /\x0d\x0a\x0d\x0a/, $_, 2; ($element) =~ /\sname="([^"]+)/i; $form{$1} = $value; } }
    This code is specifically for JPEGs and TEXT upload but could easily be modified and will also parse form elements so you end up with %image %textfile and %form.
      I had to write my own when I found out that my version of CGI.pm didn't handle uploads
      Uh, it's probably far easier to update your copy of CGI.pm than to write upload code properly. {sigh} Then you fix not only this problem, but many others solved by upgrading.

      -- Randal L. Schwartz, Perl hacker

Re: How do I upload an image?
by Anonymous Monk on Feb 12, 2004 at 22:31 UTC

    Re: How do I upload an image?

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

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

    No recent polls found