Thread: File Uploading

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    15
    Hi,

    I need to make a perl script to receive a form that contains a file upload field amongst other fields.

    From a bit of reading, I've established that using some CGI.pm magic would probably be the easiest way to go (correct me if otherwise).

    I read the CGI.pm page at search.cpan.org and there was no heading of file-uploading.

    Can anyone direct me to a quick start on how to receive and save files from file-upload fields in forms?

    Thanks,

    Josh
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>how to receive and save files from file-upload fields in forms?

    This is what I do without CGI.pm...

    #!/usr/local/bin/perl

    $img_dir = "/path/to/images";

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|n)*-->//g;
    $value =~ s/<([^>]|n)*>//g;
    $value =~ s/^s+//;
    $value =~ s/s+$//;
    $FORM{$name} = $value;
    }
    # Generate a 20 chars random image name
    @chars=("a".."n","p".."z",1..9);
    $image_name="";
    for ($i=1;$i<=20;$i++) {
    $image_name .= $chars[rand(@chars)];
    }

    $| = 1;
    $buffer =~ /^(.+)rn/;
    $bound = $1;
    @pairs = split(/$bound/,$buffer);
    @var = split(/rn/,$pairs[2]);
    @type = split(///,$var[2]);
    $image_extension = "$type[1]";

    # you can do this to see what those variables are and decide what to use
    #print "Content-type: text/htmlnn";
    #print "$pairs[0] - $pairs[0]<br>n";
    #print "$pairs[1] - $pairs[1]<br>n";
    #print "$pairs[2] - $pairs[2]<br>n";
    #print "$var[0] - $var[0]<br>n";
    #print "$var[1] - $var[1]<br>n";
    #print "$var[2] - $var[2]<br>n";
    #print "$type[0] - $type[0]<br>n";
    #print "$type[1] - $type[1]<br>n";
    #print "$type[2] - $type[2]<br>n";

    if ($pairs[2] =~ /Content-Type:/) {
    $pairs[2] =~ s/^rn.+filename.+[^w.%-]([w.%-]+)"rn(.*rn)rn//;
    $pairs[2] =~ s/rn$//;
    $image_file = "$pairs[2]";
    }
    else {
    print "Content-type: text/htmlnn";
    print "Error: Can't determine Content-Typen";
    exit(0);
    }
    # Getting variable from another form field
    if ($pairs[1] =~ /another_form_field/) {
    $pairs[1] =~ s/(.*)another_form_field"rn(.*rn)rn//;
    $pairs[1] =~ s/rn$//;
    $pairs[1] =~ s/rn//;
    $pairs[1] =~ s/(.*)another_form_field"s+//;
    }

    # Saving the image file
    open(OUTPUT,">$img_dir/$image_name.$image_extension");
    print OUTPUT $image_file;
    close(OUTPUT);
    chmod (0666,"$img_dir/$image_name.$image_extension");

    print "Content-type: text/htmlnn";
    print "Done!n";
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    15
    Hmmn... I understand the code you posted, but I would prefer a "straighter" way.

    Has anyone done it with cgi.pm?

IMN logo majestic logo threadwatch logo seochat tools logo