Falazar has asked for the wisdom of the Perl Monks concerning the following question:

I recently moved my script from UNIX to 2000 and it will not work, has anyone worked with a 2000 server and file uploads from browser? Code is here very simple, please Email me at Falazar@yahoo.com, I need this asap for work thanx
#!/usr/bin/perl print "Content-type:text/html\n\n"; use CGI qw(:standard); print "<form method='POST' enctype='multipart/form-data' action=upload.cgi> File to upload: <input type=file name=upfile><br> Notes about the file: <input type=text name=note><br><br> <input type=submit value=Press> to upload the file! </form> <bR><Br><br> <a href=test.jpg target=_blank>Link to the file</a><br> Actual file uploaded: <img src=../test.jpg><br>"; # Save Picture File if updated or new $file_name = param(upfile); if ($file_name ne "") { open(OUTF,">test.jpg") or &dienice("Couldn't open pic for writing. Please notify webmaster Falazar."); while(read($file_name,$data,1024)) { print OUTF $data; } close(OUTF); }

Replies are listed 'Best First'.
Re: Need upload Help for win 2000
by Hero Zzyzzx (Curate) on Apr 10, 2001 at 23:22 UTC

    Here's a snippet of a file upload subroutine I wrote that works pretty nice. I don't know of any particular reason that it wouldn't work on Win2k, though I haven't done perl programming on it. Make sure your directory is writable by this script.

    It runs under strict. The original does automatic duplicate checking and stores file attachment names in an mySQL database (this is part of a larger document management web app). I stripped out the mySQL parts 'cuz they didn't seem important.

    Good luck. If you know how to use subroutines this should be easy. If not, you should be able to figure it out anyway.

    You invoke it by passing it the param of the file form element, followed by the desired filename param.
    Example:
    if($q->param("attach1")){uploadfiles('attach1',$q->param('attach1filename');}

    ##### Uploads files sub uploadfiles{ my ($param,$filename)=@_; my $filepath="/www/ejc/docs/"; my ($i); my $file = $q->param($param); $filename=~ s/([^\w.-])/_/g; $filename=~ s/^[-.]*//; if ($filename =~/^([-\@\w.]+)$/){ # untaint filename $filename= $1; } open(OUT,">$filepath"."$filename") or die("Can't open outfile for +writing: $!"); while (read($file,$i,1024)) { print OUT $i; } close(OUT); } ##### End of file upload.

    Update: Changed regex, per ryanus's suggestion below.

      I had some thoughts regarding the regexes in this function. The second substitution seems to be eliminating dots and dashes from the begining of a file name. This is understandable on *nix so that no "dot files" or hidden files are created. I would however change the line from this:

      $filename=~ s/^[-.]//;
      to this:
      $filename=~ s/^[-.]*//;

      This way, all of the dots and dashes at the start of the filename are eliminated. Consider the case where $filename is equal to "..filename"

Re: Need upload Help for win 2000
by Asim (Hermit) on Apr 10, 2001 at 23:15 UTC

    Ummmm...you really need to give some more info here, Falazar. I tried it, and it worked fine. Try these steps:

    1) Run with #!/usr/bin/perl -w to turn on warnings from Perl.

    2) Put use strict; at the top of your code (after the #!/usr/bin/perl -w bit).

    3) you need to use the CGI::Carp module; put  use CGI::Carp qw(fatalsToBrowser); at the top, and read up on it, as it will oftentimes tell you exactly what error Perl sees.

    This does not look like a Windows 2000 problem, but without any error messages from you, or any idea what's going on except it fails (and works when I copy/paste the code for my WinNT/IIS combo here), I cannot help very much. Please let us know the details of where it is failing, and what the errors at are reports from the above procedures are. Also, are you using Apache, IIS, or another Web Server, as they have difference configuration issues.

    ----Asim, known to some as Woodrow.

Re: Need upload Help for win 2000
by little (Curate) on Apr 10, 2001 at 23:22 UTC
    Falazar,
    for what do you have the line :
    use CGI qw(:standard);
    in your code. You do not instantiate a cgi object, you do not use any of cgi.pm's objects functions. So I guess that you do not have a problem with cgi.pm but without :-) and your server could also simply deny file uploads. Did you check for that?

    Have a nice day
    All decision is left to your taste

    Update
    Well, you might better use a separate script to upload your files as Hero Zzyzzx provided one. While looking at yopur script I realized that you invoke your script to let it call itself in case someone wants to upload a file. That means to me that each time your script runs its running with a useless half of code :-). So better split em up into two scripts or just use the one if fileuploading is all you need.
    But I highly recommend to you to go and to read Ovid's wonderfull Web Programming with Perl course. Thats a lot of good and interesting stuff about CGI (well, and Perl).
    nuff said, enjoy your day :-)
Re: Need upload Help for win 2000
by JohnB (Scribe) on Apr 11, 2001 at 19:11 UTC

    I have also had trouble loading files on Windows 2000. The files were slightly larger with extra line feeds. Adding the line " binmode FileName " solved this problem.

    # Save Picture File if updated or new $file_name = param(upfile); if ($file_name ne "") { open(OUTF,">test.jpg") or &dienice("Couldn't open pic for writing. Please notify webmaster Falazar."); while(read($file_name,$data,1024)) { binmode OUTF; # Added this line print OUTF $data; } close(OUTF); }