Johnno,

I'm attaching a cgi script for file uploads that is pretty simple.  If
you need some information on how it works, check here:
http://www.tneoh.zoneit.com/cgi/upload/

This should get your rolling.  If you don't have CGI_LIB.pl, you can get
it from the page above.  You don't have to use it, you can go through
the CGI_LIB.pl script and take what you want from it and code it however
you want.

Justin Hopper
[EMAIL PROTECTED]


On Sat, 2002-05-18 at 03:42, Johnno wrote:
> I use a html file to browse the HD and then upload it to the web server
> 
> my $file = $req->param("FILE1");
> my $fileName = $file;
>   $fileName =~ s!^.*(\\|\/)!!;
>   $newmain = $fileName;
> 
> this contains the filename of the local hd.. ie..  c:/stuff/pic.jpg
> 
> I am wanting to upload pic.jpg to the server using the following code and
> save it as pic.jpg on the server..
> 
> open (OUTFILE, ">$newmain");
> $buffer =<OUTFILE>;
> print OUTFILE $buffer;
> close (OUTFILE);
> 
> but for some reason it only uplaod the first 21 bytes
> 
> many thanks,
>                     Johnno
> 
> 
> ----- Original Message -----
> From: "Justin Hopper" <[EMAIL PROTECTED]>
> To: "Johnno" <[EMAIL PROTECTED]>
> Cc: "perl-unix-users mailing list"
> <[EMAIL PROTECTED]>
> Sent: Saturday, May 18, 2002 3:46 PM
> Subject: Re: [Perl-unix-users] file uploads in Perl
> 
> 
> > Johnno,
> >
> > I think you are going to have to give a bit more information than that.
> > Where are you trying to upload the file to?  The code below is a little
> > confusing.  It looks like you opened the file $file for writing, then
> > tried to read the information into the var $buffer, then tried to output
> > the contents of $buffer to $file?  I'm not sure what would happen here,
> > but I think you would just end up with an empty file for $file.
> >
> > Explain what you are trying to do, and I'd be happy to help you if I
> > can.  Maybe somebody will answer my previous question about perlcc and
> > using IO::SOCKET =)
> >
> > Justin Hopper
> > [EMAIL PROTECTED]
> >
> >
> > On Fri, 2002-05-17 at 20:38, Johnno wrote:
> > > how do you upload a file in perl??
> > >
> > > I have var $file which is the file on the local HD..  from there is
> where a
> > > get into trouble..
> > >
> > > open (OUTFILE, ">$file");
> > > $buffer =<OUTFILE>;
> > > print OUTFILE $buffer;
> > > close (OUTFILE);
> > >
> > >
> > > Johnno
> > >
> > >
> > > _______________________________________________
> > > Perl-Unix-Users mailing list
> > > [EMAIL PROTECTED]
> > > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> >
> >
> > _______________________________________________
> > Perl-Unix-Users mailing list
> > [EMAIL PROTECTED]
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

#!/usr/local/bin/perl
require 'CGI_LIB.pl';
# For those whose server is on windows system, you need the following three lines.
# Thanks to Carlo Maria Piacenti <[EMAIL PROTECTED]> who told me about this.
binmode(STDIN);
binmode(STDOUT);
binmode(STDERR);
&Parse_Multi;
if (defined $CGI{'SeeUploadedFile'}) {
  # During your testing on your web server, you can check the size of 
  # ./uploadfile.unknown with the size of the tested file.
  # If you're using windows system, binmode function is needed for saving
  # binary data and binmode function should come after open function.
  open(TMP, "> uploadfile.unknown");
  binmode(TMP);
  print TMP $CGI{'UploadedFile'}->{'Contents'};
  close(TMP);
  print "Content-Type: $CGI{'UploadedFile'}->{'Content-Type'}\n";
  # The following will default the filename when the browser asking user to save it.
  # This only works in netscape.
  print "Content-Length: ".length($CGI{'UploadedFile'}->{'Contents'})."\n";
  print "Content-Disposition: filename=\"$CGI{'UploadedFile'}->{'filename'}\n\n";
  print $CGI{'UploadedFile'}->{'Contents'};
}
elsif (defined $CGI{'SeeCGIHash'}) {
  &Print_Head;
  print "<html><head><title>Values of Hash Array \%CGI</title></head>";
  print "<body><h2>Values of Hash Array \%CGI</h2><form OnSubmit=\"return ";
  print "false;\"><input type=\"submit\" OnClick=\"history.go(-1)\" value";
  print "=\"Back\"></form><hr>";
  for $key (sort keys %CGI) {
    print "<blockquote><table border=\"1\" cellspacing=\"2\" cellpadding=\"4\"><tr>";
    if (ref($CGI{$key}) =~ /HASH/) {
      print "<th colspan=\"2\">Hash array of \"<font color=\"#009900\">$key";
      print "</font>\"<tr><th>Variable Name<th>Value";
      for (keys %{$CGI{$key}}) {
        print "<tr><td valign=\"top\"><font color=\"#0000ff\">\$CGI{'$key'}->";
        print "{'$_'}</font><td><font  color=\"#ff3333\">$CGI{$key}->{$_}";
	print "</font>";
      }
    }
    elsif (ref($CGI{$key}) =~ /ARRAY/) {
      $count = 0;
      print "<th colspan=\"2\">Array of \"<font color=\"#009900\">$key</font>";
      print "\"<tr><th>Variable Name<th>Value";
      for $count (0..$#{$CGI{$key}}) {
        print "<tr><td><font color=\"#0000ff\">\$CGI{'${key}'}\[$count\]";
	print "</font><td><font color=\"#ff3333\">$CGI{$key}[$count]</font>\n";
      }
    }
    else {
      print "<th colspan=\"2\">Single value of \"<font color=\"#009900\">$key";
      print "</font>\"<tr><th>Variable Name<th>Value";
      print "<tr><td><font color=\"#0000ff\">\$CGI{'$key'}</font><td><font ";
      print "color=\"#ff3333\">$CGI{$key}</font>\n";
    }
    print "</table></blockquote><p>";
  }
  print "<hr><form OnSubmit=\"return false\"><input type=\"submit\" OnClick";
  print "=\"history.go(-1)\" value=\"Back\"></form></body></html>";
}

Reply via email to