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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
perlperlAuthor Commented:
can you please tell me how to do that. i am using CGI/Perl
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; }
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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

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

<form action="your/url" method="POST" enctype="multipart/form-data">
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.