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
perlperlAsked:
Who is Participating?
 
ahoffmannCommented:
1) you need a cgi which reads the uploaded data and saves it to a file
2) I guess you can, but this has nothing to do with web servers, CGI or internet at all
0
 
perlperlAuthor Commented:
can you please tell me how to do that. i am using CGI/Perl
0
 
ahoffmannCommented:
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; }
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
perlperlAuthor Commented:
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
0
 
ahoffmannCommented:
> 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
0
 
perlperlAuthor Commented:
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....
0
 
perlperlAuthor Commented:
i also tried
while (my $more=read( $q->param('upload_file'),  $buffer, 8192 ))

not working
0
 
perlperlAuthor Commented:
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
0
 
perlperlAuthor Commented:
Any comments?
0
 
ljo8877Commented:
Check that the form has enctype="multipart/form-data".

<form action="your/url" method="POST" enctype="multipart/form-data">
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.