Link to home
Start Free TrialLog in
Avatar of hatecapletters
hatecapletters

asked on

submitting file from HD

when a CGI is called with a full path to a file on the viewers hard disk, how do i open that file (actually upload it) and then save it on my server where i want it to be ?
Avatar of thoellri
thoellri

HATECAPLETTERS :-)

try this one. I assume you're using CGI.pm. Copy somewhere into cgi-bin area (for example as upload.pl) then use:

   http://your.server.com/cgi-bin/upload.pl

which will bring up a form with a single filefield. Select "Browse ..." to find a file to upload. Click "Load File" to call the cgi again, this time with the file-upload data. Your file will be displayed in on the generated web-page (Better make sure it's not too big :-). Instead of "print $buffer;" you could open an output file and send the data there.

Hope this helps
  Tobias



#!/usr/local/bin/perl
use strict;
no strict 'refs';
use CGI;

my $cgi=new CGI;
my $f=$cgi->param('filename');

if (defined($f) && $f) {
  my($n,$bytesread,$buffer);
  print $cgi->header;
  print $cgi->start_html;
  print "<b>here is your uploaded file <i>$f</i>:</b>\n";
  print "<hr><pre>";
  while ($bytesread=read($f,$buffer,1024)) {
    print $buffer;
    $n+=$bytesread;
  }
  print "</pre><hr>";
  print "<b>file size: <i>$n</i> bytes</b>\n";
  print $cgi->end_html;
} else {
  print $cgi->header;
  print $cgi->start_html;
  print $cgi->start_multipart_form();
  print "File: ";
  print $cgi->filefield(-name=>'filename',
                        -default=>"",
                        -size=>50,-maxlength=>100)," ";
  print $cgi->submit('submit','Load File');
}
Try to use cgi-lib.pl

http://cgi-lib.stanford.edu/cgi-lib/

There is example for upload file either under perl 4 or 5.
Avatar of hatecapletters

ASKER

hmmm, actually I wanted to know how to program it, not what pre-programmed modules to use :-)
Well, then take a look at the sources for the CGI-module and reinvent the wheel ;-)

Tobias

hmm, guess I have to clarify what it is I wanna do. On a web page I have a form with a lot of standard fields. One of the fields is the file handling window of the browser, which, when the form is submitted to the CGI with "post" carries a scalar value with the full path to the file on the viewer's system. Now I want to "fetch" that file, after having check that it meets the requirements, and save it on my server. What I need to know is the exact use of the filehandle for the new file im creating and, if there's no way around it, how to get and implement CGI.pm. I have looked at some scripts using read($filename,$buffer,1024) and print newfilehandle $buffer.. that's as far as I have gotten.

Thanks in advance

hatecapletters
I guess what you want is something like this

use CGI;

my $req = new CGI;
my $file = $req->param("yourField");

open (OUTFILE, ">$OutFileName");
while (my $bytesread = read($file, my $buffer, 1024)) {
   print OUTFILE $buffer;
}
close (OUTFILE);

As I said before, I used "cgi-lib.pl" from http://cgi-lib.stanford.edu/cgi-lib/

when you call the  '&ReadParse', it will handle everything for you, inluding the filehandle part. Before that you'll need to set the $cgi_lib'writefiles to a path to enable the file copying function. Initially the file is copied to \tmp, but I customised it to get the file name uploaded and to the path i want.
here's what I did:

use CGI;

my $req = new CGI;
my $file = $req->param("filename");

open(outfile,">test.gif");
while (my $bytesread = read($file, my $buffer, 1024)) {
 print outfile $buffer;
}
close(outfile);

"filename" is the name of the form field  of type=file
I have entered test.gif as outfilename. I have tried using the system path to the directory where i wanted the file saved, and i have used the relative URL to the directory..nothing works, the script just returns an empty page, no erors, but also, no test.gif saved to the directory i specified )or anywhere else).. what am i doing wrong ?

oh, and how can I read the filename the viewer submits before uploading it  (to check for type etc )
I'm getting more and more confused.

Why don't you just take my script I posted in the first place? What's wrong with it?

It shows you how to access the filename of the posted file:

   print "<b>here is your uploaded file <i>$f</i>:</b>\n";

and it shows you how to save the file (well this part 'prints' to the browser, but I'm sure you can send that to a file):

   while ($bytesread=read($f,$buffer,1024)) {
     print $buffer;
     $n+=$bytesread;
   }

Whats wrong with it?

Tobias

 


ok, i managed to piece something together i can use. Sn8776  please post an answer, and i'll give you the points , as you answer came the closest to what i wanted to do :-)

take care all :-)

hatecapletters
ASKER CERTIFIED SOLUTION
Avatar of sn8776
sn8776

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
no prob, im pretty tied up myself .-) as i said , i got it to run in the end, thanks