Link to home
Start Free TrialLog in
Avatar of perlperl
perlperl

asked on

upload file

in my cgi script, i have small part of html code, <input type = file> where user specifies file name

1) how can i upload user's .doc file and copy it on my server.
2) finally can i copy the same file on client machine
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

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
Avatar of perlperl
perlperl

ASKER

can you please tell me how to do that. i am using CGI/Perl
1) something like:

use CGI:
my $q = new CGI;
my $bytesread       = 0;
my $buffer          = '';
open( FID, '>', "/path/to/file" ) or die "cannot open file";
while (my $more=read( $q->param{'file'}, $buffer, 8192 )) { $bytesread+=$more; print FID $buffer; }
i tried

#!c:/perl/bin/Perl.exe
use CGI;

print "Content-type: text/html; charset=iso-8859-1\n\n";
$|=1;

my $q = new CGI;
my $bytesread       = 0;
my $buffer          = '';
$file = $q->param('upload_file');

print "$file";

open( FID, "> $file" ) or die "cannot open file";

while (my $more=read( $file, $buffer, 8192 ))
{
      $bytesread+=$more;
      print FID $buffer;
      }

it creates file c:\\test.doc on server but it is not copying the contents of test.doc form clients machine to server.
the file is empty on server may be because its not going in whike loop
> open( FID, "> $file" )
dooh, that's dangereous, don't use it this way, hardcode the filename in your CGI.

> .  is not copying the contents ..
you probably used the wrong variable for reading
i tried this

#!c:/perl/bin/Perl.exe
use CGI;

print "Content-type: text/html; charset=iso-8859-1\n\n";
$|=1;

my $q = new CGI;
my $bytesread       = 0;
my $buffer          = '';
$file = "c:\\test.doc";

print "$file";

open( FID, "> $file" ) or die "cannot open file";

while (my $more=read( $file,  $buffer, 8192 ))
{
      $bytesread+=$more;
      print FID $buffer;
      }

still it is not reeading the file c:\test.doc from client....
i also tried
while (my $more=read( $q->param('upload_file'),  $buffer, 8192 ))

not working
i lso tried

open( FID, "> $file" ) or die "cannot open file";
binmode (FID);

didn't work

is the header issue which cannot read local file on client's machine
Any comments?
Check that the form has enctype="multipart/form-data".

<form action="your/url" method="POST" enctype="multipart/form-data">