Andi: / (CGI) Upload + Forumlar

Beitrag lesen

Hallo Stefan,

wenn ich Dich jetzt richtig verstanden habe, dann willst Du in einem Rutsch sowohl Textfelder aus Formularen, sowie eine Datei schicken und dann entsprechend verarbeiten!!

Das sollte mit folgendem Script gehen...

Ciao, Andi

####################################################
#!/usr/bin/perl

$quota = 100;  #in KB

$content_type = $ENV{'CONTENT_TYPE'};
$content_len = $ENV{'CONTENT_LENGTH'};
$request_method = $ENV{'REQUEST_METHOD'};

#Formulardaten einlesen
&ParseForm;

#Die Daten aus dem Formular
$filename = $FORM{'filename'}; #z.B. Upload-Verzeichnis+Name
$bin   = $FORM{'Datei'};    #Die eigentliche Datei

print "Content-type:   text/html\n\n";
#Filesize cheecken

$filesize = $content_len/1024;
#if ($filesize > $quota) { #Behandlung des Quotas}

&SaveFile;

print "<html><body>Upload OK!</body></html>";

exit;

#------------------------------------------------------------------------------
sub ParseForm
#------------------------------------------------------------------------------

This piece of code is obtained from Tod Sambar for reading multipart

form data. See http://www.sambar.com

{
# Buffer the POST content
binmode STDIN;
read(STDIN, $buffer, $content_len);

# find boundary
   # Eric Poulsen fixed the following to allow for quotes.
   # ($boundary = $content_type) =~ s/^.*boundary=(.*)$/\1/;
   ($boundary = $content_type) =~ s/^.*boundary="?(.*?)"?$/$1/;
          @pairs = split(/--$boundary/, $buffer);
          @pairs = splice(@pairs,1,$#pairs-1);
          for $part (@pairs)
           {
            ($dump,$fline,$value) = split(/\r\n/,$part,3);
            next if $fline =~ /filename=""/;
            $fline =~ s/^Content-Disposition: form-data; //;
            (@columns) = split(/;\s+/, $fline);
            ($name = $columns[0]) =~ s/^name="([^"]+)"$/$1/g;
            if ($#columns > 0)
             {
              if ($value =~ /^Content-Type:/)
               {
                ($contenttype,$dump,$value) = split(/\r\n/,$value,3);
               }
              else
               {
                ($dump,$value) = split(/\r\n/,$value,2);
               }
             }
            else
             {
              ($dump,$value) = split(/\r\n/,$value,2);
              if (grep(/^$name$/, keys(%CGI)))
               {
                if (@{$FORM{$name}} > 0)
                 {
                  push(@{$FORM{$name}}, $value);
                 }
                else
                 {
                  $arrvalue = $FORM{$name};
                  undef $FORM{$name};
                  $FORM{$name}[0] = $arrvalue;
                  push(@{$FORM{$name}}, $value);
                 }
               }
              else
               {
                next if $value =~ /^\s*$/;
                $FORM{$name} = $value;
               }
              next;
             }
            $FORM{$name} = $value;
           }
}

#--------------------------------------------------------------------------
sub SaveFile
#--------------------------------------------------------------------------
{
open(FILE, ">$filename");
binmode FILE;

print FILE $bin;  
close FILE;  

}