Steve

Steven K. Mariner

Last Updated: 22-Mar-2024

| Mariner Home | Kristie | Steve | Triona | Cressa | skmpu@bhmk.com |

| eBay | Programming | Wargaming | Music | RenFaire | Motorcycles | Fidonet | skmpu@bhmk.com |

CGI File Upload Perl Code Snippet

Nothing gets a programmer up to speed faster than a focused, functional code snippet.

So you have a web page and you want to permit file uploads with it, and Perl is available as your CGI language. Obviously, you're going to need to do a little bit on your web page and a little bit of Perl:

Web Page Snippet

Key points:

     <FORM METHOD="POST" ACTION="upload.pl" ENCTYPE="multipart/form-data">
          File: <INPUT TYPE="FILE" NAME="srcfnm" SIZE=30 MAXLENGTH=255>
          <INPUT TYPE="SUBMIT" VALUE="Upload"> <INPUT TYPE="RESET">
     </FORM>

Perl Snippets

First, we have to grab the filename the user ultimately filled into the form:

     use CGI;

     $query = CGI::new();
     $srcfnm = $query->param("srcfnm");

Second, we need to strip off the directory information so we're merely writing the output file in the current directory. I think there's a CPAN module that will do this portably; the snippet below is possibly Windows-specific. It also uses substr(), which most hardcore Perl programmers shun, favoring instead the use of regular expressions. Do it how you like; this snippet is here strictly to emphasize the importance of stripping off the directory stuff that the user sends you:

     # Convert filename to local filename
     $endcol=length($srcfnm);
     $stacol=$endcol;
     $staflg=0;

     while (!$staflg) {
          if (!$stacol) { last; }
          $curchr = substr($srcfnm,$stacol-1,1);
          if ($curchr eq '\\') { last; }
          if ($curchr eq '/') { last; }
          if ($curchr eq ':') { last; }
          $stacol--;
     }
     $desfnm = substr($srcfnm,$stacol);

Finally, the snippet you were really looking for, which reads the user-specified file in from the web services standard input device and writes it to the disk in binary mode.

**NOTE** I believe this is often called EZ Upload or EZ File Transfer, and I believe it is only supported in Netscape v2.0 or higher and Internet Explorer v4.0 or higher. I don't know what versions of other browsers support this feature, and I'm not sure what happens when you use it in an earlier version of these browsers.

      $inpfsz = 0;
      open(RESFIL, ">$desfnm");
      binmode(RESFIL);

      while ($inpcnt=read($srcfnm,$inpbuf,2096)) {
            $inpfsz += $inpcnt;
            print RESFIL $inpbuf;
      }

      close(RESFIL);

And that's it. With the example set by these three Perl snippets (and the HTML snippet as well), you should be able to set up file uploads on your web page.

Don't forget to ensure that your script has both read and write access to the directory where you'll be writing the files. Sometimes you have to go through your web-hosting provider to get some permissions changed.

| eBay | Programming | Wargaming | Music | RenFaire | Motorcycles | Fidonet | skmpu@bhmk.com |

| Mariner Home | Kristie | Steve | Triona | Cressa | skmpu@bhmk.com |

© 1997-2024, Steven K. Mariner