<!-- SophiaKnows -->

PERL PARSE MULTIPART FORM
Rev 2: December 2004

#!/usr/local/bin/perl

#	MULTI-PART FORM PARSER

#	This script parses the content posted from a Multi-part HTML form
#	including the contents of one or more desktop files streamed in the
#	posted data


sub Parse_MultipartForm {
   $bodystring="";
   $boundry=$ENV{'CONTENT_TYPE'};
   $boundry=~s/^multipart\/form-data; boundary=//;
   @lines=<STDIN>;
   foreach $line (@lines) {
      chop($line);
      $bodystring .= "$line"."\n";
       }

   @SubmitArray=split($boundry,$bodystring);
   @partslist=($bodystring=~/Content-Disposition: form-data; ([^\n]*)/g);

   for($p=0;$p<$#SubmitArray-1;$p++) {
      $thisPart=$partslist[$p];
      $Partnames[$p]=&ReturnPartName($partslist[$p]);
      $Partfilename{$Partnames[$p]}=&ReturnPartFileName($partslist[$p]);
      unless($Partfilename{$Partnames[$p]}) {
         $Partvalues{$Partnames[$p]}=&ReturnPartValue($SubmitArray[($p+1)]);
         } else {
            $Partvalues{$Partnames[$p]}=&ReturnPartContents($SubmitArray[($p+1)]);
            }
      }
   }


sub ReturnPartName {
   my @partNames=($_[0]=~/name=\"([^\"]*)\"/g);;
   return $partNames[0];
   }

sub ReturnPartFileName {
   my @partFileNames=($_[0]=~/filename=\"([^\"]*)\"/g);;
   $thisPartFileName=$partFileNames[0];
   $thisPartFileName=~s/^.+\\(\w+\.\w+)$/$1/;
   return $thisPartFileName;
   }

sub ReturnPartValue {
   $theValue=$_[0];
   $theValue=~s/Content-Disposition: form-data; ?[^\n]*//;
   $theValue=~s/Content-Type: [^\n]*//;
   $theValue=~s/[\10\13\11\r\n]/ /g;
   $theValue=~s/ ?-+$//;
   $theValue=~s/  +/ /;
   $theValue=~s/^ +//;
   $theValue=~s/ +$//;

   return $theValue;
   }

sub ReturnPartContents {
   $theValue=$_[0];
   $theValue=~s/Content-Disposition: form-data; ?[^\n]*//;
   $theValue=~s/Content-Type: [^\n]*//;
   return $theValue;
   }


1;

< CODEBASE | TOP^ | MAINPAGE >

Text & Design By Tony Pisarra
© SophiaKnows 1998-2004