09. april 2002 - 17:09
#10
Fordi jeg er sådan en flink dreng, får du hermed en smule kode jeg bruger i et messageboard system:
sub parse_form
{
# Initialize variables
my ($key, $value, $buffer, @chunks, $chunk, %in, $read, $length, $method, $type);
# Get some information about the input
$method = $ENV{'REQUEST_METHOD'};
$length = $ENV{'CONTENT_LENGTH'};
$type = $ENV{'CONTENT_TYPE'};
# Raise an error if the max post data limit is exceeded
&prog_error("The post containing $length bytes is illegal. Admin has limited data receival to $o{'max_data'}")
if ($o{'max_data'} > 0) && ($length > $o{'max_data'});
# Needed for some OS'
if ($OS eq 'win' or $OS eq 'vms' or $OS eq 'os2') {
binmode(STDIN);
binmode(STDOUT);
binmode(STDERR);
}
# If we are dealing with an "ordinary" post, get or head request.
if ($method eq 'GET' || $method eq 'HEAD' || $type eq 'application/x-www-form-urlencoded') {
if ($method eq 'GET' || $method eq 'HEAD') {
@chunks = split(/[&;]/, $ENV{'QUERY_STRING'});
}
elsif ($method eq 'POST') {
($read = read(STDIN, $buffer, $length) == $length) or &prog_error("Couldn't read to the end. Wanted to read $length but only got $read");
@chunks = split(/&/, $buffer);
}
else {
&prog_error("The request method $method is invalid!");
}
foreach $chunk (@chunks) {
($key, $value) = split(/=/, $chunk, 2);
$key =~ tr|+| |;
$key =~ s|%([a-fA-F0-9]{2})|pack("c", hex($1))|ge;
$value =~ tr|+| |;
$value =~ s|%([a-fA-F0-9]{2})|pack("c", hex($1))|ge;
$value =~ s,\|,|,g;
exists $in{$key} ? ($in{$key} .= "~|$value") : ($in{$key} = $value);
}
return %in;
# Process multipart form data. First figure out what the boundary is used.
} elsif ($type =~ m|^multipart/form-data; boundary=(.+)$|) {
my $boundary = $1;
# Ensure that the request originates from a POST form
($method eq 'POST') or &prog_error("The request method is invalid for a multipart/form-data request!");
# Read the data
($read = read(STDIN, $buffer, $length) == $length) or &prog_error("Couldn't read to the end. Wanted to read $length but only got $read");
# Iterate over each boundary in our buffer
CHUNK: foreach $chunk (split /$boundary/, $buffer) {
chomp $chunk;
(defined $chunk) or next CHUNK;
($chunk eq "--") and next CHUNK;
(not $chunk) and next CHUNK;
# Retrieve headers and associated data, delimited by two \r\n
# Then cut away \r from our header.
my ($header, $data) = split (/\015\012\015\012/, $chunk, 2);
$header =~ s/\015\012/\012/g;
my ($key, $value);
my $mime = "text/plain";
foreach my $head (split /\012/,$header) {
my ($headname, $headdata) = split(/: /, $head);
($headname =~ m/^Content-Type$/i) and $mime = $headdata;
($headname =~ m/^Content-Disposition$/i) or next;
# The following part is bad coding, and it is so because of my laziness
# Read file contents into memory and work from there. A more prudent approach
# would be to break it up into read parts later, and save those read parts.
# This would avoid large files to be read entirely into memory. To be changed...
foreach my $dispair (split /; /, $headdata) {
($dispair eq 'form-data') and next;
my ($dispkey, $dispvalue) = split(/=/, $dispair, 2);
$dispvalue =~ s/^\"//;
$dispvalue =~ s/\"$//;
$key = $dispvalue if ($dispkey eq 'name');
($dispkey eq 'filename') ? ($value = $dispvalue and $in{'file'}{$key} = $data)
: ($data =~ s|\015\012--$||o and $value = $data);
$value =~ s,\|,Ѐ,g;
$in{$key} = $value;
}
}
}
return %in;
}
}
09. april 2002 - 17:12
#11
Oh, jeg glemte at fortælle at de uploadede filer gemmes i $in{'file'}{'feltnavn'} variablen. Alle andre inputs bliver gemt i $in{'feltnavn'}. Brug kun det her kode hvis du er sikker på at folk ikke uploader 100mb filer eller sådan noget i den stil, da det bliver gemt i hukommelsen!